[英]How to get const_iterator when the T is 'const std::map',?
I have the following struct:我有以下结构:
template<typename T>
struct Foo
{
typename T::iterator iter;
};
The expected type:预期类型:
iter
is a std::map<K, V>::iterator
when T
is deduced as std::map<K, V>
.当
T
被推导为std::map<K, V>
时, iter
是一个std::map<K, V>::iterator
。
iter
is a std::map<K, V>::const_iterator
when T
is deduced as const std::map<K, V>
当
T
推导为const std::map<K, V>
时, iter
是std::map<K, V>::const_iterator
But my code always get a std::map<K, V>::iterator
.但是我的代码总是得到一个
std::map<K, V>::iterator
。
How to achieve the expected implementation?如何实现预期的执行?
You can provide a std::conditional
type:您可以提供一个
std::conditional
类型:
#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>;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.