因为C ++ 17弃用了std::iterator
,所以我需要实现自己的。事实证明它只是一个带有一些typedef的空结构。
template <class Category, class Type, class Diff = ptrdiff_t,
class Pointer = Type*, class Reference = Type&>
struct iterator {
typedef Category iterator_catergory;
typedef Type value_type;
typedef Diff difference_type;
typedef Pointer pointer;
typedef Reference reference;
};
struct dummy_iter : iterator<std::output_iterator_tag, int> {}
这有效:
printf("%d\n", std::is_same<typename dummy_iter::pointer, int*>::value);
但是,它不适用于std::iterator_traits
:
printf("%d\n", std::is_same<typename std::iterator_traits<dummy_iter>::pointer, int*>::value);
error: 'pointer' in 'struct std::iterator_traits<dummy_iter>' does not name a type
答案 0 :(得分:3)
你有一个错字。这句话:
typedef Category iterator_catergory;
应改为:
typedef Category iterator_category;
该标准指定除非iterator_traits
包含所有必需的typedef,否则它将变为空类。见[iterator.traits] / 2:
如果Iterator具有有效的([temp.deduct])成员类型difference_type, value_type,pointer,reference和iterator_category, iterator_traits应公开具有以下内容 无障碍成员:
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;
否则,iterator_traits将不包含任何成员 以上名称。