默认模板参数 - 不一定要放在右边?为什么有效?

11

默认模板参数是否可以以非从右开始的方式使用“默认值”?

标准是什么?
编译器会如何解释?

例如,我很惊讶这段代码可以工作

#include <iostream>
using namespace std;

template <bool T=true, class U>   //"default" from LEFT-most parameter
void f(U u){
    if(T){ cout<<true;}
    else cout<<false;
}
int main() {
    auto x = []( ){  };
    f(x);
    return 0;
}

点此查看实时演示:https://ideone.com/l6d9du

2个回答

11
这里模板参数推导很有效,因为对于函数模板,后续的模板参数可以通过函数参数进行推导。在这种情况下,模板参数 U 可以从函数参数 u 推导出来。请注意,对于类模板,如您所期望的那样,在默认模板参数之后的后续模板参数必须具有默认模板参数或者是一个模板参数包。
参考链接:§14.1/11 Template parameters [temp.param]

If a template-parameter of a class template, variable template, or alias template has a default template-argument, each subsequent template-parameter shall either have a default template-argument supplied or be a template parameter pack. If a template-parameter of a primary class template, primary variable template, or alias template is a template parameter pack, it shall be the last template-parameter. A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list ([dcl.fct]) of the function template or has a default argument ([temp.deduct]). A template parameter of a deduction guide template ([temp.deduct.guide]) that does not have a default argument shall be deducible from the parameter-type-list of the deduction guide template. [ Example:

template<class T1 = int, class T2> class B;   // error

// U can be neither deduced from the parameter-type-list nor specified
template<class... T, class... U> void f() { } // error
template<class... T, class U> void g() { }    // error

— end example ]

您可以尝试使U不可推导,并观察会发生什么:
template <bool T=true, class U>   //"default" from LEFT-most parameter
void f(){
    if(T){ cout<<true;}
    else cout<<false;
}
int main() {
    f();            // Fail. Can't deduce U.
    f<true>();      // Fail. Can't deduce U.
    f<true, int>(); // Fine. T=true, U=int.
    return 0;
}

请注意,您必须显式指定所有模板参数,以使代码正常工作,这使得默认模板参数完全无意义。如果您想要使 f()f<true>() 正常工作,则需要为 U 也提供一个默认的模板参数(或者将其作为模板参数包) 。
template <bool T=true, class U=int>
void f(){
    if(T){ cout<<true;}
    else cout<<false;
}
int main() {
    f();              // Fine. T=true,  U=int
    f<false>();       // Fine. T=false, U=int
    f<false, char>(); // Fine. T=false, U=char
    return 0;
}

5
您可以为任何参数提供默认值。
如果您想使用默认值,您不能显式地指定在有默认值的参数右侧的参数。但是,在您的示例中,'U' 是从函数参数的类型推导出来的,而 'T' 则被设置为默认值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接