1

The Issue


I've read pointers are a type of an iterator and they can be used with the STL algorithms. Does that mean that they implement iterator traits?

I think not because the code

int a = 5, *p = &a;
std::cout << p.value_type << std::endl;

emits error: request for member ‘value_type’ in ‘p’, which is of non-class type ‘int*’, which means that int* isn't even a class.

Summary


  1. how are pointers used in STL algorithms without iterator traits?
  2. If pointers can go without implementing those, can I create a custom iterator that doesn't have iterator traits? (Do STL algorithms even use those traits?)
  3. When I use a pointer as an iterator, should I do something special to use it? (for example, define iterator traits...)
snatchysquid
  • 1,283
  • 9
  • 24
  • [The standard library provides partial specializations for pointer types `T*`, which makes it possible to use all iterator-based algorithms with raw pointers.](https://en.cppreference.com/w/cpp/iterator/iterator_traits#Specializations) – Evg Sep 17 '21 at 07:33
  • Use `std::iterator_traits` like `std::iterator_traits::value_type`. – songyuanyao Sep 17 '21 at 07:35
  • @Evg Thanks! So I guess that answers 1,2. And about 3, I don't have to do anything and it will be considered that I fully support (random access) iterators, right? – snatchysquid Sep 17 '21 at 07:36
  • @songyuanyao use them where? In the code snippet? – snatchysquid Sep 17 '21 at 07:36
  • About 3, right. Try: `typeid(std::iterator_traits::value_type).name()`. – Evg Sep 17 '21 at 07:43
  • @Evg Whereshould I place this line and why is it needed? – snatchysquid Sep 17 '21 at 07:48
  • 2
    Instead of `p.value_type` in your code example to show `value_type` (it's a type!). – Evg Sep 17 '21 at 07:49

0 Answers0