Questions tagged [iterator-traits]

42 questions
37
votes
1 answer

What are the typical use cases of an iterator_trait

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 struct iterator_traits { typedef typename…
san
  • 4,144
  • 6
  • 32
  • 50
34
votes
1 answer

Contiguous iterator detection

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…
alfC
  • 14,261
  • 4
  • 67
  • 118
22
votes
4 answers

Why the value_type/difference_type/pointer/reference of back_insert_iterator/front_insert_iterator/insert_iterator are all void?

In my project, I want to split stream into some given type of values, so I implement a template function as template TOutputIter SplitSpace(std::istream& IS, TOutputIter result) { TElem elem; while (IS…
Yun Huang
  • 4,256
  • 7
  • 27
  • 36
21
votes
2 answers

const_iterator and constness of const_iterator::value_type

Why in STL std::iterator_traits::value_type is the same type as std::iterator_traits::value_type Why it is designed like that? Shouldn't the first be const T and the second only T? How are you supposed to take the underlying const…
Sogartar
  • 2,035
  • 2
  • 19
  • 35
13
votes
3 answers

Why can't I get value_type from iterator_traits?

I'm doing this: const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; const auto foo = cbegin(arr); const typename iterator_traits::value_type bar = 1; I would have expected bar to have the type int. But instead I'm…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
11
votes
4 answers

specializing iterator_traits

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…
imre
  • 1,667
  • 1
  • 14
  • 28
11
votes
2 answers

Why do algorithms use iterator_traits::value_type instead of iter::value_type?

In an algorithm, I can determine the value_type directly from the iterator via iter::value_type. Why do algorithms use iterator_traits to do the same? #include #include #include #include using namespace…
Hardik
  • 149
  • 6
11
votes
2 answers

Why does reverse_iterator doubly define its nested types?

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…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
10
votes
1 answer

Are Forward-Iterators Output-Iterators?

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].
MFH
  • 1,664
  • 3
  • 18
  • 38
7
votes
1 answer

Canonical way to define forward output iterator

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…
MFH
  • 1,664
  • 3
  • 18
  • 38
5
votes
3 answers

Is There an Example of an Iterator Which Wouldn't use ptrdiff_t as its difference_type?

I see that iterator_traits always defines a difference_type: https://en.cppreference.com/w/cpp/iterator/iterator_traits#Member_types I'm just wondering why, wouldn't that be ptrdiff_t for every type? Is there an example of an iterator which doesn't…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
4
votes
1 answer

Template parameters not used in partial specialization

I have the following code: template > class Carray { // ... typedef T* pointer; typedef pointer iterator; // ... }; Now I'm trying to do partial specialization for iterator_traits.…
orlp
  • 112,504
  • 36
  • 218
  • 315
4
votes
1 answer

How to avoid specializing iterator_traits for each possible instantiation of a templated iterator?

I want to pimp up my ranged based for loops, for example by enabling reversed iteraton. I managed to get it working to some degree by writing an adaptor, but I am lost on how to make the adpaptor composable. #include template
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
4
votes
1 answer

std::iterator_traits divergence between libstdc++ and libc++

Given: struct Iter { using value_type = int; using difference_type = int; using reference = int; using pointer = int; using iterator_category = int; }; The following works fine with libstc++, but fails to compile against libc++…
jotik
  • 17,044
  • 13
  • 58
  • 123
4
votes
2 answers

Can one make move_iterator from istream_iterator?

Consider following code: typedef istream_iterator char_itr ; char_itr eos; string ll("some text here"); istringstream line_in(ll); char_itr start(line_in); move_iterator mstart(start); // !!! move_iterator
Piotr G
  • 959
  • 1
  • 7
  • 25
1
2 3