Search code examples
c++templatesargumentsparameter-passingplaceholder

C++ template placeholders not permitted in function arguments


In the following C++ code, a template placeholder in argument of function fun1, and in the return type of function ret1, does not compile:

template <typename T = int>
class type {
    T data;
};

void fun1(type      arg); // Error: template placeholder not permitted in this context 
void fun2(type<>    arg); // Ok
void fun3(type<int> arg); // Ok

type      ret1(); // Error: Deduced class type 'type' in function return type
type<>    ret2(); // Ok
type<int> ret3(); // Ok

int main() {
    type      var1;  // Ok!!!!!!
    type<>    var2;  // Ok
    type<int> var3;  // Ok
}

But, var1 is ok.

  • Why does var1 compile, but fun1 and ret1 do not?
  • Is there any logic behind this inconsistent behavior between function declarations and variable declarations?

Solution

  • var1 benefits from CTAD, where all non-defaulted template arguments (i.e. none) can be deduced from the initialisation. Both function declarations however, are not candidates for CTAD, so the template argument list must be supplied even if that list is empty.

    Deduction for class templates

    Implicitly-generated deduction guides

    When, in a function-style cast or in a variable's declaration, the type specifier consists solely of the name of a primary class template C (i.e., there is no accompanying template argument list), candidates for deduction are formed as follows:
    ...

    (emphasis added)