Carucel
Carucel

Reputation: 481

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?

Upvotes: 1

Views: 307

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93264

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());

Upvotes: 5

Related Questions