Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ template placeholders not permitted in function arguments

The following C++ code with 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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading