Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default template parameter - don't have to be from right? Why it works?

Does default template parameter can use "default value" in a way that does NOT start from right?

What is the criteria?
How will the compiler interpret?

For example, I am so surprised that this code works.

#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;
}

See live demo here : https://ideone.com/l6d9du

like image 255
javaLover Avatar asked Aug 25 '16 05:08

javaLover


People also ask

Can template have default parameters?

Template parameters may have default arguments. The set of default template arguments accumulates over all declarations of a given template.

Can default argument be used with the template class?

Can default arguments be used with the template class? Explanation: The template class can use default arguments.

Why do we use template template parameter?

8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.

Can we pass non-type parameters to templates?

Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.


2 Answers

Template argument deduction works well here, because for function template, the subsequent template arguments might be deduced by function arguments. In this case, template argument U could be deduced from function argument u. Note that for class template, as you expected, the subsequent template-parameter after a default template-argument shall either have a default template-argument or be a template parameter pack.

§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 ]

You could try to make U undeducible and see what will happen:

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;
}

Note you have to specify all the template arguments explicitly to make the code work, which makes default template arguments meaningless at all. If you want to make f() or f<true>() work, you need to give U a default template-argument too (or make it template parameter pack).

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;
}
like image 125
songyuanyao Avatar answered Sep 28 '22 08:09

songyuanyao


You can supply a default for any parameter.

If you want to use the default, you can't explicitly specify parameters to the right of the defaulted parameter. However, in your example, U is being deduced from the type of the argument to the function, and T is being defaulted.

like image 45
Martin Bonner supports Monica Avatar answered Sep 28 '22 06:09

Martin Bonner supports Monica