Home > Software design >  C template placeholders not permitted in function arguments
C template placeholders not permitted in function arguments

Time:11-24

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?

CodePudding user response:

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)

CodePudding user response:

type var1; is using Class Template Argument Deduction (CTAD), which is possible since C 17 and deduces the template arguments from the initializer of the variable (here an empty argument list).

This is not possible in a function declaration, because there is no initializer from which to deduce the template arguments.

In particular for a function parameter, there wouldn't be any possible source to determine the template arguments from (except if such a function would be considered to be a template similar to the C 20 abbreviated function templates).

For the return type, it could have been specified similar to how the auto placeholder return type works, but that simply wasn't done as part of the introduction of CTAD.

  • Related