- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定:
struct Iter {
using value_type = int;
using difference_type = int;
using reference = int;
using pointer = int;
using iterator_category = int;
};
以下代码适用于 libstc++,但无法针对 libc++ 5.0.0 进行编译:
#include <iterator>
#include <type_traits>
static_assert(
std::is_same<
std::iterator_traits<Iter>::iterator_category,
Iter::iterator_category
>::value, "");
出现错误:
error: no member named '
iterator_category
' in 'std::__1::iterator_traits<Iter>
'std::is_same<std::iterator_traits<Iter>::iterator_category, Iter::iterator_category>::value, "");
如果Iter::iterator_category
,则静态断言成功是标准输入类别之一,例如std::input_iterator_tag
.
恕我直言,它不应该失败,因为 C++ 草案在 [iterator.traits]#2 中声明:
If Iterator has valid ([temp.deduct]) member types
difference_type
,value_type
,pointer
,reference
, anditerator_category
,iterator_traits<Iterator>
shall have the following as publicly accessible members:using difference_type = typename Iterator::difference_type;
using value_type = typename Iterator::value_type;
using pointer = typename Iterator::pointer;
using reference = typename Iterator::reference;
using iterator_category = typename Iterator::iterator_category;Otherwise,
iterator_traits<Iterator>
shall have no members by any of the above names.
谁能解释一下这是否是一个实现错误,或者为什么我的期望是错误的?
最佳答案
我们也有,在[std.iterator.tags] :
It is often desirable for a function template specialization to find out what is the most specific category of its iterator argument, so that the function can select the most efficient algorithm at compile time. To facilitate this, the library introduces category tag classes which are used as compile time tags for algorithm selection. They are:
input_iterator_tag
,output_iterator_tag
,forward_iterator_tag
,bidirectional_iterator_tag
andrandom_acces_iterator_tag
. For every iterator of typeIterator
,iterator_traits<Iterator>::iterator_category
shall be defined to be the most specific category tag that describes the iterator's behavior.namespace std {
struct input_iterator_tag { };
struct output_iterator_tag { };
struct forward_iterator_tag: public input_iterator_tag { };
struct bidirectional_iterator_tag: public forward_iterator_tag { };
struct random_access_iterator_tag: public bidirectional_iterator_tag { };
}
int
不是这些标签之一,所以 iterator_traits<Iter>::iterator_category
不能还给你int
.我建议拥有一个无效的迭代器类别只是违反了 iterator_traits
的前提条件。 - 这并不一定意味着库一定会失败,但也不意味着失败是库错误。
但是,这些先决条件在 [iterators] 中没有像在库部分的其他部分中那样明确说明。所以我倾向于建议这两个库都是正确的,但是 libc++ 不在 iterator_traits<Iter>
中定义任何成员别名的方法。可能更好。
关于c++ - std::iterator_traits libstdc++ 和 libc++ 之间的分歧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513930/
我有两个包含 40000 个样本的数据集。我想用 python 计算这两个数据集之间的 Kullback-Leibler 散度。在 python 中有什么有效的方法吗? 最佳答案 编辑: 好的。我发现
我正在尝试编译一个 .c 文件,用于处理 mMIPS 指令集中硬件中的裁剪,但是我似乎在“results=sfu1”行处收到错误“宏参数数量不一致” ' 当我尝试使用 lcc -o mips_mem.
这个正则表达式 /{(\w+)}/g 应该匹配大括号 {} 之间 的每个单词字符。相反,我在 Regex101 JavaScript engine 中得到了不同的结果和 Chrome 控制台。 Reg
我是一名优秀的程序员,十分优秀!