Search code examples
c++templatesdefault-parameters

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


Solution

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