如何编写类型特征“is_container"或“is_vector"?
是否可以编写一个值对所有常见 STL 结构(例如,vector
、set
、map
、map
、...)?
首先,我想编写一个类型特征,它对 vector
为真,否则为假.我试过这个,但它不能编译:
template结构 is_vector {static bool const value = false;};模板<T类,U类>struct is_vector>>::类型>{static bool const value = true;};
错误信息是模板参数未用于部分特化:U
.
看,另一个基于 SFINAE 的用于检测类 STL 容器的解决方案:
template结构 is_container : std::false_type {};模板结构 is_container_helper {};模板struct is_container<,std::conditional_t<错误的,is_container_helper<类型名称 T::value_type,类型名称 T::size_type,类型名称 T::allocator_type,类型名 T::iterator,类型名 T::const_iterator,decltype(std::declval().size()),decltype(std::declval().begin()),decltype(std::declval().end()),decltype(std::declval().cbegin()),decltype(std::declval().cend())>,空白>>:公共 std::true_type {};
当然,您可以更改要检查的方法和类型.
如果您只想检测 STL 容器(这意味着 std::vector
、std::list
等),您应该执行类似 这个.
更新.正如@Deduplicator 所指出的,容器可能不满足 AllocatorAwareContainer 要求(例如:std::array
).这就是为什么不需要检查 T::allocator_type
的原因.但是您可以以类似的方式检查任何/所有Container要求.>
Is it possible to write a type trait whose value is true for all common STL structures (e.g., vector
, set
, map
, ...)?
To get started, I'd like to write a type trait that is true for a vector
and false otherwise. I tried this, but it doesn't compile:
template<class T, typename Enable = void>
struct is_vector {
static bool const value = false;
};
template<class T, class U>
struct is_vector<T, typename boost::enable_if<boost::is_same<T, std::vector<U> > >::type> {
static bool const value = true;
};
The error message is template parameters not used in partial specialization: U
.
Look, another SFINAE-based solution for detecting STL-like containers:
template<typename T, typename _ = void>
struct is_container : std::false_type {};
template<typename... Ts>
struct is_container_helper {};
template<typename T>
struct is_container<
T,
std::conditional_t<
false,
is_container_helper<
typename T::value_type,
typename T::size_type,
typename T::allocator_type,
typename T::iterator,
typename T::const_iterator,
decltype(std::declval<T>().size()),
decltype(std::declval<T>().begin()),
decltype(std::declval<T>().end()),
decltype(std::declval<T>().cbegin()),
decltype(std::declval<T>().cend())
>,
void
>
> : public std::true_type {};
Of course, you might change methods and types to be checked.
If you want to detect only STL containers (it means std::vector
, std::list
, etc) you should do something like this.
UPDATE. As @Deduplicator noted, container might not meet AllocatorAwareContainer requirements (e.g.: std::array<T, N>
). That is why check on T::allocator_type
is not neccessary. But you may check any/all Container requirements in a similar way.
相关文章