c++ - Variadic 模板模板和 SFINAE

标签 c++ c++11 templates variadic-templates

我正在玩弄 SFINAE,我尝试检查我的输入是否由各种类型的输入组成。 clang 提供的错误没有多大帮助。你有什么想法吗?

谢谢

struct IsFree
{
};

template <typename _Type, typename _State>
struct Input
{
};

template <typename... _Inputs>
struct Inputs
{
};

template <template <typename _Type, typename _State> class, typename... _Inputs>
struct Inputs<Input<_Type, _State>, _Inputs...> : public Inputs<_Inputs...>
{
};

其他地方:

auto temp = Inputs<Input<float, IsFree>, Input<float, IsFree>> {};

我开始使用 clang-5.0 和 -std=c++17 :

13 : <source>:13:21: error: use of undeclared identifier '_Type'
struct Inputs<Input<_Type, _State>, _Inputs...> : public Inputs<_Inputs...>
                    ^
13 : <source>:13:35: error: expected a type
struct Inputs<Input<_Type, _State>, _Inputs...> : public Inputs<_Inputs...>
                                  ^
2 errors generated.
Compiler exited with result code 1

最佳答案

template <template <typename _Type, typename _State> class, typename... _Inputs>
struct Inputs<Input<_Type, _State>, _Inputs...> : public Inputs<_Inputs...>
{
};

需要

template <typename _Type, typename _State, typename... _Inputs>
struct Inputs<Input<_Type, _State>, _Inputs...> : public Inputs<_Inputs...>
{
};

在模式中Input<_Type, _State> _Type 和 _State 只是类型通配符,您只需要 template <typename, typename> class F template 模板参数语法,如果您需要使用通配符匹配模板模板参数。在这种情况下,您将模板与名为 Input 的已知模板进行匹配

关于c++ - Variadic 模板模板和 SFINAE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47434046/

相关文章:

c++ - 具有继承性的可变参数模板和默认值

c++ - 为什么在这种情况下不调用我的 move 构造函数? (首先分配 r 值引用并创建对象)

c++ - 使用花括号初始化临时聚合对象

带有 C++ std::map 的 Python dict.update()?

c++ - 为什么bernoulli_distribution::param_type 的构造函数是显式的?

java - 哪些模板语言可同时用于 Java 和 Javascript?

c++ - 插入数据后无法删除 unsigned char*

android - 如何确定性地使用 std::this_thread::yield() ?

c++ - 如何创建可以采用函数指针和 lambda 的模板函数

c++ - 如何覆盖模板中的运算符