可变模板可以与非可变模板参数匹配吗
Can a variadic template match a non-variadic template parameter?
本文关键字:参数 更新时间:2023-10-16
考虑以下片段:
template<template<class> class T,class U>
struct apply
{
typedef T<U> type;
};
typedef apply<std::tuple,int>::type tuple_of_one_int; //Should be std::tuple<int>
GCC 4.8.2。说:
type/value mismatch at argument 1 in template parameter list for [...] struct apply
expected a template of type ‘template<class> class T’, got ‘template<class ...> class std::tuple’
这基本上意味着像std::tuple
这样的可变模板不是apply
中T
的有效模板参数。
这是GCC错误还是标准强制要求这种行为?
如果我错了,有人会纠正我,但这句话似乎是正确的:
3当模板参数的相应类模板的模板参数列表中的每个模板参数或[FI11]模板别名模板(称为A)与p 的模板参数表中的相应模板参数匹配时,模板参数与模板模板参数(称为p)匹配
A
(给定的模板)必须将它的每个模板参数与P
的模板模板匹配。
从本节的第二部分中,我们了解到限制并不适用于相反的情况,这意味着包含参数包的模板模板可以匹配任何内容。
当p的模板参数列表包含模板参数包(14.5.3)时,模板参数包将与a的模板参数-列表中的零个或多个模板参数或模板参数包匹配,其类型和形式与p 中的模板参数包相同
正如你可能已经知道的方法,使其工作是
template<template<class> class T,class U>
struct apply
{
typedef T<U> type;
};
template<class T> using tuple_type = std::tuple<T>;
typedef apply<tuple_type,int>::type tuple_of_one_int;
c++11标准也有一个与您的示例等效的示例。
template <class ... Types> class C { /∗ ... ∗/ };
template<template<class> class P> class X { /∗ ... ∗/ };
X<C> xc; //ill-formed: a template parameter pack does not match a template parameter
最后一条注释完全描述了您的情况,类C
在这种情况下相当于std::tuple
。
您的代码格式错误,标准中有等效的示例(14.3.3/2下):
...
template <class ... Types> class C { /∗ ... ∗/ };
template<template<class> class P> class X { /∗ ... ∗/ };
...
X<C> xc; // ill-formed: a template parameter pack does not match a template parameter
...
修复:
template<template<class...> class T,class U>
struct apply
{
typedef T<U> type;
};
typedef apply<std::tuple,int>::type tuple_of_one_int;
typedef apply<std::vector,int>::type vector_of_int;
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用