Calling a function with an argument implicitly convertible to an object of template class

Viewed 158

Consider the following example (godbolt):

template <typename T>
struct S {
    S(int) {}
};

template <typename T>
void f(S<T>, T) {}

int main() {
    f(1, 2);
}

Compiling it gives the following error:

<source>: In function 'int main()':
10 : <source>:10:11: error: no matching function for call to 'f(int, int)'
     f(1, 2);
           ^
7 : <source>:7:6: note: candidate: template<class T> void f(S<T>, T)
 void f(S<T>, T) {}
      ^
7 : <source>:7:6: note:   template argument deduction/substitution failed:
10 : <source>:10:11: note:   mismatched types 'S<T>' and 'int'
     f(1, 2);
           ^

Making S a non-template makes the example compile.

Why doesn't this code compile despite an implicit conversion from int to S<T>?

1 Answers
Related