在variadic pack之前的C++ 11默认模板参数

tow*_*120 0 c++ templates variadic-templates c++11

为什么模板类参数在参数之后具有默认值,但在variadic之前也必须具有默认值? 实例

template<class A = int, class B, class ...Args>     // B must have default argument
struct Dat{};
Run Code Online (Sandbox Code Playgroud)

另一方面,如果A没有默认参数,那么ok:

template<class A, class B, class ...Args>         // B must have default argument
struct Dat{};
Run Code Online (Sandbox Code Playgroud)

dyp*_*dyp 6

这与可变参数模板无关:

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

X<int, double> // instantiates X with First == int, Second == double
Run Code Online (Sandbox Code Playgroud)

这些参数intdouble用于填充的参数First,Second由左到右.第一个参数指定第一个参数,第二个参数指定第二个参数.当参数具有默认值时,您不需要指定参数:

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

X<int> // instantiates X with First == int, Second == double
Run Code Online (Sandbox Code Playgroud)

如果您现在有一个没有默认参数的第三个参数,则无法使用默认参数:

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

X<int, char> // tries to instantiate X with First == int, Second == char,
             // Third == ??
Run Code Online (Sandbox Code Playgroud)

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

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}
Run Code Online (Sandbox Code Playgroud)

在OP的例子中:

template<class A = int, class B, class ...Args> struct Dat {};
         ^~~~~~~        ^~~~~~~  ^~~~~~~~~~~~~
         |              |        |no argument required
         |              |an argument IS required
         |no argument required
Run Code Online (Sandbox Code Playgroud)

所以你总是要为第二个参数提供一个参数,因此,你需要为第一个参数提供一个参数.不能使用第一个参数的默认参数.