我正在编写一个自定义矢量类:
#include <iterator>
template <typename T>
class vector {
public:
using value_type = T;
using pointer = value_type*;
using iterator = pointer;
using const_iterator = const iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
auto crbegin() const -> const_reverse_iterator {
return const_reverse_iterator(data_);
}
private:
pointer data_{};
};
int main(int argc, char* argv[]) {
vector<int> v;
auto i = v.crbegin();
return 0;
}
编译上面的代码时,我收到此错误(在 GCC 和 MSVC 上):
error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<int* const>’
当我将反向迭代器定义更改为std::reverse_iterator<const T*>
. 相比有什么区别std::reverse_iterator<const_iterator>
?