可变模板可以与非可变模板参数匹配吗

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这样的可变模板不是applyT的有效模板参数。

这是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;