Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can the template not be instantiated in this piece of C++ code?

As titled, why is the template instantiation in the main function wrong? I use Clang++ with flag "-std=c++2a". Did I use anything in a wrong way here?

template<int, int>
void f(int) { std::cout << "int"; };

template<class T> 
void g() {  // dependent-name.
  f<0, T()>(0);
}

int main() {
  g<int>();  // wrong here, why?
}

Error messages from Clang:

test.cc:41:3: error: no matching function for call to 'f'
  f<0, T()>(0);
  ^~~~~~~~~
test.cc:48:3: note: in instantiation of function template specialization 'g<int>' requested here
  g<int>(); // "int";
  ^
test.cc:35:6: note: candidate template ignored: invalid explicitly-specified argument for 2nd
      template parameter
void f(int) { std::cout << "int"; };
     ^
1 error generated.
like image 375
Jason Yu Avatar asked Aug 19 '20 15:08

Jason Yu


1 Answers

This instantiation:

f<0, T()>(0);

for T=int gives int() for the second explicit template parameter. This is basically a vexing parse, and so the argument is treated as a function type. This is not the same as an int object, which is what f expects for its template parameters.

Instead, you can instantiate it like this:

f<0, T{}>(0);

so that the second template parameter is an int object.

Here's a demo.

like image 154
cigien Avatar answered Sep 17 '22 22:09

cigien