Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;11变量包前的默认模板参数_C++_Templates_C++11_Variadic Templates - Fatal编程技术网

C++ C++;11变量包前的默认模板参数

C++ C++;11变量包前的默认模板参数,c++,templates,c++11,variadic-templates,C++,Templates,C++11,Variadic Templates,为什么模板类参数后面的参数有默认值,前面的变量也必须有默认值? 这与可变模板无关: template<class First, class Second> struct X {}; X<int, double> // instantiates X with First == int, Second == double 如果现在第三个参数没有默认参数,则无法使用默认参数: template<class First, class Second = double&g

为什么模板类参数后面的参数有默认值,前面的变量也必须有默认值?


这与可变模板无关:

template<class First, class Second> struct X {};

X<int, double> // instantiates X with First == int, Second == double
如果现在第三个参数没有默认参数,则无法使用默认参数:

template<class First, class Second = double> struct X {};

X<int> // instantiates X with First == int, Second == double
template<class First, class Second = double, class Third> struct X {};

X<int, char> // tries to instantiate X with First == int, Second == char,
             // Third == ??
template<class First, class Second = double, class... Pack> struct X {};

X<int> // instantiates X with First == int, Second == double, Pack == (empty)
X<int, char> // First == int, Second == char, Pack == (empty)
X<int, char, short, bool> // First == int, Second == char, Pack == {short, bool}
模板结构X{};
X//尝试用First==int,Second==char实例化X,
//第三==??

模板参数包可以为空,因此它可以跟随带有默认参数的参数:

template<class First, class Second = double> struct X {};

X<int> // instantiates X with First == int, Second == double
template<class First, class Second = double, class Third> struct X {};

X<int, char> // tries to instantiate X with First == int, Second == char,
             // Third == ??
template<class First, class Second = double, class... Pack> struct X {};

X<int> // instantiates X with First == int, Second == double, Pack == (empty)
X<int, char> // First == int, Second == char, Pack == (empty)
X<int, char, short, bool> // First == int, Second == char, Pack == {short, bool}
模板结构X{};
X//实例化X,第一个==int,第二个==double,Pack==(空)
X//First==int,Second==char,Pack==(空)
X//First==int,Second==char,Pack=={short,bool}

在OP的示例中:

template<class A = int, class B, class ...Args> struct Dat {};
         ^~~~~~~        ^~~~~~~  ^~~~~~~~~~~~~
         |              |        |no argument required
         |              |an argument IS required
         |no argument required
template struct Dat{};
^~~~~~~        ^~~~~~~  ^~~~~~~~~~~~~
|| |无需任何参数
||需要一个参数
|不需要任何参数

因此,您必须始终为第二个参数提供参数,因此,您也需要为第一个参数提供参数。无法使用第一个参数的默认参数。

这与可变模板无关:

template<class First, class Second> struct X {};

X<int, double> // instantiates X with First == int, Second == double
如果现在第三个参数没有默认参数,则无法使用默认参数:

template<class First, class Second = double> struct X {};

X<int> // instantiates X with First == int, Second == double
template<class First, class Second = double, class Third> struct X {};

X<int, char> // tries to instantiate X with First == int, Second == char,
             // Third == ??
template<class First, class Second = double, class... Pack> struct X {};

X<int> // instantiates X with First == int, Second == double, Pack == (empty)
X<int, char> // First == int, Second == char, Pack == (empty)
X<int, char, short, bool> // First == int, Second == char, Pack == {short, bool}
模板结构X{};
X//尝试用First==int,Second==char实例化X,
//第三==??

模板参数包可以为空,因此它可以跟随带有默认参数的参数:

template<class First, class Second = double> struct X {};

X<int> // instantiates X with First == int, Second == double
template<class First, class Second = double, class Third> struct X {};

X<int, char> // tries to instantiate X with First == int, Second == char,
             // Third == ??
template<class First, class Second = double, class... Pack> struct X {};

X<int> // instantiates X with First == int, Second == double, Pack == (empty)
X<int, char> // First == int, Second == char, Pack == (empty)
X<int, char, short, bool> // First == int, Second == char, Pack == {short, bool}
模板结构X{};
X//实例化X,第一个==int,第二个==double,Pack==(空)
X//First==int,Second==char,Pack==(空)
X//First==int,Second==char,Pack=={short,bool}

在OP的示例中:

template<class A = int, class B, class ...Args> struct Dat {};
         ^~~~~~~        ^~~~~~~  ^~~~~~~~~~~~~
         |              |        |no argument required
         |              |an argument IS required
         |no argument required
template struct Dat{};
^~~~~~~        ^~~~~~~  ^~~~~~~~~~~~~
|| |无需任何参数
||需要一个参数
|不需要任何参数

因此,您必须始终为第二个参数提供参数,因此,您也需要为第一个参数提供参数。无法使用第一个参数的默认参数。

在默认参数之后不能有非默认参数。这是一种语言,默认参数对于不显式传递一个参数非常有用。例如
模板结构X{};X/*==X*/
。参数用于始终以相同的顺序填充参数,因此,如果需要为下一个参数传递参数,则无法使用默认参数——您需要为两个参数传递参数。可变模板参数与此无关()。在默认参数之后不能有非默认参数。这是一种语言,默认参数对于不显式传递一个参数非常有用。例如
模板结构X{};X/*==X*/
。参数用于始终以相同的顺序填充参数,因此如果需要为下一个参数传递参数,则无法使用默认参数——您需要为两个参数传递参数。可变模板参数与此无关()。