https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124357
Bug ID: 124357
Summary: Type inference depends on the order of arguments
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: d7d1cd at mail dot ru
Target Milestone: ---
Here's the code:
$ cat source.cc
int bar(int, char) { return 0; }
int bar(double, long) { return 1; }
template <typename T, typename U>
int foo(T t, int (*)(T, U)) { return bar(t, 0); }
int main() {
return foo(42, &bar);
}
It compiles successfully, and the compiler infers the types correctly.
But if you swap the function arguments, the inference stops working, even
though nothing has changed:
$ cat source.cc
int bar(int, char) { return 0; }
int bar(double, long) { return 1; }
template <typename T, typename U>
int foo(int (*)(T, U), T t) { return bar(t, 0); }
int main() {
return foo(&bar, 42);
}
$ g++ source.cc -std=c++23
<source>: In function 'int main()':
<source>:8:15: error: no matching function for call to 'foo(<unresolved
overloaded function type>, int)'
8 | return foo(&bar, 42);
| ~~~^~~~~~~~~~
• there is 1 candidate
• candidate 1: 'template<class T, class U> int foo(int (*)(T, U), T)'
<source>:5:5:
5 | int foo(int (*)(T, U), T t) { return bar(t, 0); }
| ^~~
• template argument deduction/substitution failed:
• couldn't deduce template parameter 'U'
<source>:8:15:
8 | return foo(&bar, 42);
| ~~~^~~~~~~~~~