iterator-traits

Why does reverse_iterator doubly define its nested types?

老子叫甜甜 提交于 2019-12-19 05:48:33
问题 It appears that the iterator adaptor reverse_iterator doubly defines most of its nested types. In particular, it inherits publicly from std::iterator which exposes iterator_category , value_type , difference_type , pointer and reference . Except for iterator_category and value_type , these are all explicitly typedef 'ed again in the class definition. 24.5.1.1 Class template reverse_iterator [reverse.iterator] namespace std { template <class Iterator> class reverse_iterator : public iterator

Can one make move_iterator from istream_iterator?

。_饼干妹妹 提交于 2019-12-13 13:31:21
问题 Consider following code: typedef istream_iterator<char> char_itr ; char_itr eos; string ll("some text here"); istringstream line_in(ll); char_itr start(line_in); move_iterator<char_itr> mstart(start); // !!! move_iterator<char_itr> meos(eos); vector<char> vc(mstart, meos); Above code will not compile because of line (!!!): error C2440: 'return' : cannot convert from 'const char' to 'char &&' But if you replace mstart and meos with start and eos , respectively (regular iterators), the code

Are Forward-Iterators Output-Iterators?

二次信任 提交于 2019-12-10 12:42:22
问题 Are ForwardIterators required to be OutputIterators? My current STL-implementation (VS2012) derives forward_iterator_tag from both input_iterator_tag and output_iterator_tag , but I can't find this requirement in the standard [N3485]. 回答1: In C++11, no, forward iterators are not required to be output iterators. The output iterator requirements are like an extra set of requirements that an iterator can have, regardless of the rest of the iterator requirements it meets. Forward iterators are

What are the typical use cases of an iterator_trait

北城以北 提交于 2019-12-09 07:52:06
问题 I am new to C++ so please bear with me. I am trying to understand STL iterator_traits. In the book "The C++ Standard Library" the structure iterator_traits is defined as follows: template <class T> struct iterator_traits{ typedef typename T::value_type value_type typedef typename T::difference_type difference_type typedef typename T::iterator_category iterator_category typedef typename T::pointer pointer typedef typename T::reference reference } So it seems to me that it is re-exposing the

What are the typical use cases of an iterator_trait

最后都变了- 提交于 2019-12-03 09:47:19
I am new to C++ so please bear with me. I am trying to understand STL iterator_traits. In the book "The C++ Standard Library" the structure iterator_traits is defined as follows: template <class T> struct iterator_traits{ typedef typename T::value_type value_type typedef typename T::difference_type difference_type typedef typename T::iterator_category iterator_category typedef typename T::pointer pointer typedef typename T::reference reference } So it seems to me that it is re-exposing the subtypes that T already exposes. Moving ahead further, the book gives an example of how to use it, which

Template parameters not used in partial specialization

坚强是说给别人听的谎言 提交于 2019-11-28 13:55:15
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray { // ... typedef T* pointer; typedef pointer iterator; // ... }; Now I'm trying to do partial specialization for iterator_traits . It seems OK to me, but g++ 4.4.5 complains: #include <iterator> namespace std { template<typename T, typename Allocator> struct iterator_traits<typename Carray<T, Allocator>::iterator> { // line 128 typedef T value_type; typedef typename Allocator::difference_type difference_type; typedef typename Allocator::reference reference; typedef typename Allocator::pointer

specializing iterator_traits

时光毁灭记忆、已成空白 提交于 2019-11-28 11:16:33
I'd like to specialize std::iterator_traits<> for iterators of a container class template that does not have the usual nested typedefs (like value_type , difference_type , etc.) and whose source I shouldn't modify. Basically I'd like to do something like this: template <typename T> struct iterator_traits<typename Container<T>::iterator> { typedef T value_type; // etc. }; except that this doesn't work, as the compiler is unable to deduce T from Container<T>::iterator . Is there any working way to achieve the same? For example: template <typename T> class SomeContainerFromAThirdPartyLib {

Canonical way to define forward output iterator

旧城冷巷雨未停 提交于 2019-11-28 08:02:03
问题 How does one define forward-output-iterators in C++11 in a canonical way? According to the standard a forward_iterator is only a input_iterator. So the corresponding forward_iterator_tag only extends input_iterator_tag . If we are using std::iterator to define our iterators, what tag do we use for a forward-output-iterator? Is it canonical to define a private tag that extends both forward_iterator_tag and output_iterator_tag or is there a better solution? 回答1: The canonical thing to do is to

Contiguous iterator detection

旧城冷巷雨未停 提交于 2019-11-27 21:07:49
C++17 introduced the concept of ContiguousIterator http://en.cppreference.com/w/cpp/iterator . However it doesn't seem that there are plans to have a contiguous_iterator_tag (in the same way we now have random_access_iterator_tag ) reported by std::iterator_traits<It>::iterator_category . Why is contiguous_iterator_tag missing? Is there a conventional protocol to determine if an iterator is Contiguous? Or a compile time test? In the past I mentioned that for containers if there is a .data() member that converts to a pointer to ::value type and there is .size() member convertible to pointer

Template parameters not used in partial specialization

随声附和 提交于 2019-11-27 08:01:44
问题 I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray { // ... typedef T* pointer; typedef pointer iterator; // ... }; Now I'm trying to do partial specialization for iterator_traits . It seems OK to me, but g++ 4.4.5 complains: #include <iterator> namespace std { template<typename T, typename Allocator> struct iterator_traits<typename Carray<T, Allocator>::iterator> { // line 128 typedef T value_type; typedef typename Allocator::difference_type