Search code examples
c++decltype

Get the type of the iterator using `decltype`


I want to get the type of the iterator on objects of (template)type A using

typedef decltype(A::begin) A_iterator;

However, this gives a

"cannot determine which instance of overloaded function "std::vector<_Ty, _Alloc>::begin" is intended"

when A is a std::vector<...>.

I think the compiler cannot distinguish between the const function begin and the non-const function begin. How can I choose between these two?


Solution

  • Assuming A is a type, and not a variable identifier.

    using A_iterator = decltype(std::declval<A>().begin());
    

    Or just...

    using A_iterator = typename A::iterator;
    

    If A is a variable identifier:

    using A_iterator = decltype(A.begin());