What type of __iter_concept<_Iter>

Viewed 100

I watched std::random_access_iterator and other iterator concepts
This is what the GCC implementation looks like

template<typename _Iter>
    concept random_access_iterator = bidirectional_iterator<_Iter>
      && derived_from<__detail::__iter_concept<_Iter>,
              random_access_iterator_tag>
      && totally_ordered<_Iter> && sized_sentinel_for<_Iter, _Iter>
      && requires(_Iter __i, const _Iter __j,
          const iter_difference_t<_Iter> __n)
      {
    { __i += __n } -> same_as<_Iter&>;
    { __j +  __n } -> same_as<_Iter>;
    { __n +  __j } -> same_as<_Iter>;
    { __i -= __n } -> same_as<_Iter&>;
    { __j -  __n } -> same_as<_Iter>;
    {  __j[__n]  } -> same_as<iter_reference_t<_Iter>>;
      };

How is it that __iter_concept<_Iter>, derived from random_access_iterator_tag ?

2 Answers

How is it that __iter_concept<_Iter>, derived from random_access_iterator_tag?

Because it's written to be. __iter_concept is not a concept; it's a type (or type alias). C++20 specifies a set of rules for determining the iterator category (forward, random access, input, etc) from a valid iterator that implements the C++20 concept-ified iterator category. The name for these rules is confusingly named "ITER_CONCEPT".

The word "concept" here is to denote that it is using the C++20 concept rules for this computation, not the C++17 pre-concepts rules.

__detail::__iter_concept<T> is the template metaprogramming type in your GCC standard library that implements ITER_CONCEPT. Hence the name. It generates a type that is inherited from the ITER_CONCEPT-defined type, so that the concepts that use it can use derived_from to detect the iterator type.

Symbols starting with __ or _Capital are reserved for your c++ compiler and std library to use for internal implementation details. (Use of such symbols anywhere else is banned by the standard, unless allowed by your compiler explicitly).

So that template is some internal implementation detail used to implement the C++ standards requirements for that concept.

__detail::__iter_concept<_Iter>

this is the std library internal implementation's way to get a tag for a given type. It is probably defined elsewhere in the std headers as C++ code. How it works, I would read the source code. Or rather, I would just presume it does what it says.

The naive method would just be to use the std iterator_traits.

Related