I have the following struct:
template<typename T>
struct Foo
{
typename T::iterator iter;
};
The expected type:
iteris astd::map<K, V>::iteratorwhenTis deduced asstd::map<K, V>.iteris astd::map<K, V>::const_iteratorwhenTis deduced asconst std::map<K, V>
But my code always get a std::map<K, V>::iterator.
How to achieve the expected implementation?
CodePudding user response:
You can provide a std::conditional type:
#include <type_traits> // std::conditional_t
template<typename T>
struct Foo {
using iter = std::conditional_t<std::is_const_v<T>
, typename T::const_iterator
, typename T::iterator>;
};
