lundi 27 août 2018

Data type of default value for template function argument different from instantiating data type

 #include <iostream>
 #include <string>

 template <typename T>
 void f(T x = std::string{""})
 {
   std::cout << '[' << x << ']' << std::endl;
 }

 int main()
 {
   f(23); // Case 1: Doesn't fail
   f<int>(); // Case 2: Compilation error
   f<int>(23); // Case 3: Doesn't fail
 }

Shouldn't the Case 1 and Case 3 also fail, because the function template is instantiated by int and the deafult value is of type std::string.

Aucun commentaire:

Enregistrer un commentaire