I have the following struct:
template<typename T>
struct Foo
{
typename T::iterator iter;
};
The expected type:
iter
is astd::map<K, V>::iterator
whenT
is deduced asstd::map<K, V>
.iter
is astd::map<K, V>::const_iterator
whenT
is 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>;
};