检查所有T型的参数包 [英] Check a parameter pack for all of type T

查看:67
本文介绍了检查所有T型的参数包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乔纳森·韦克利(Jonathan Wakely)对问题答案要检查参数包中的所有类型是否可复制构造/ 29603857#29603857>检查参数包中所有类型是否可复制构造的类型特征提供一种简单的方法来检查参数包中扩展的所有变量是否都属于同一类型-例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆