检查所有T型的参数包 [英] Check a parameter pack for all of type T
问题描述
乔纳森·韦克利(Jonathan Wakely)对问题
Jonathan Wakely's answer to the question Type trait to check that all types in a parameter pack are copy constructible gives a simple(ish) way to check if all of the variables expanded in a parameter pack are of the same type - eg:
#include <type_traits>
namespace detail {
enum class enabler {};
}
template <bool Condition>
using EnableIf =
typename std::enable_if<Condition, detail::enabler>::type;
template<typename... Conds>
struct and_ : std::true_type {};
template<typename Cond, typename... Conds>
struct and_<Cond, Conds...>
: std::conditional<Cond::value, and_<Conds...>,
std::false_type>::type {};
template<typename... T>
using areInts = and_<std::is_same<T,int>...>;
template<typename... T>
using areMySpecificClass = and_<std::is_same<T,MySpecificClass>...>;
我不知道如何扩展它,写一个像 areTypeT
。
I can't work out how to extend this, to write a template like areTypeT
, for example.
我第一次偶然发现参数包'T'必须在模板参数列表的末尾 。我最近的尝试可以编译,但是如果使用它,则会出现替换失败:
My first attempts stumbled on "Parameter pack 'T' must be at the end of the template parameter list". My more recent attempt compiles, but if I use it then I get substitution failures:
template<typename Target>
template<typename... T1>
using areT = and_<std::is_same<T1,Target>...>;
如何进行这项工作?
推荐答案
您的语法有点差,您不需要两个单独的模板声明,该语法用于定义类外的成员模板:
Your syntax is just off a bit, you don't need two separate template declarations, that syntax is for defining member templates out-of-class:
template<typename Target, typename... Ts>
using areT = and_<std::is_same<Ts,Target>...>;
static_assert(areT<int,int,int,int>::value,"wat");
static_assert(!areT<int,float,int,int>::value,"wat");
这篇关于检查所有T型的参数包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!