| Issue |
235330
|
| Summary |
[clang] Partial ordering for the address of a function template skips an `auto` return type on one side only
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
MichaelSteffens
|
Clang accepts the address of an overloaded function template when partial ordering must
fail. The case needs one candidate with a placeholder (`auto`) return type. Outside a call,
Clang skips `auto` in the return type of P, but compares `auto` in A as a type. GCC rejects
the reproducer as ambiguous.
Reproducer (`repro.cpp`, no includes):
```cpp
template <class T> auto f(T) {}
template <class T> void f(T *);
void (*p)(int *) = &f;
```
```
$ clang++ -std=c++20 -fsyntax-only repro.cpp # exit 0
$ g++ -std=c++20 -fsyntax-only repro.cpp
repro.cpp:3:21: error: converting overloaded function 'f' to type 'void (*)(int*)' is ambiguous
```
Expected: an error, because the address is ambiguous. Actual: Clang selects `f<int>` of
`void f(T *)`. The `-ast-dump` of `p` shows a `DeclRefExpr` to the second template. A
`static_assert(sizeof(T) == 0)` in the body of `void f(T *)` fires.
### All cases in one unit (Compiler Explorer, `-std=c++20`)
```cpp
// Expected (standard): errors in a, b and c; d is well-formed.
namespace a { // clang accepts and selects f(T*); expected: ambiguous
template <class T> auto f(T) {}
template <class T> void f(T *);
void (*p)(int *) = &f;
}
namespace b { // clang rejects as ambiguous: correct
template <class T> void f(T) {}
template <class T> auto f(T *) {}
void (*p)(int *) = &f;
}
namespace c { // clang accepts and selects void f(T*); expected: ambiguous
template <class T> auto f(T *) {}
template <class T> void f(T *);
void (*p)(int *) = &f;
}
namespace d { // call: both compilers select f(T*): correct
template <class T> auto f(T) {}
template <class T> void f(T *);
void g(int *q) { f(q); }
}
```
GCC 15 reports errors in `a`, `b` and `c`. Clang reports only `b`. Cases `a` and `b` differ
only in which template returns `auto`. In case `c` the parameter types are equal, so Clang
orders the two templates by the return type alone. Case `d` is correct: in a call, partial
ordering uses the parameter types only ([temp.deduct.partial]/3.1).
### Standard
Paragraph numbers are from C++20 (N4861). C++23 and the current draft keep these rules.
1. [over.over]/5 eliminates a specialization only if the template of another one is more
specialized ([temp.func.order]). Exactly one function must remain.
2. [temp.deduct.partial]/3.3: outside a call, partial ordering uses the function type, so the
return types take part.
3. [temp.func.order]/3 replaces only template parameters with unique types. `auto` stays `auto`.
4. [temp.deduct.type]/1 requires the deduced A to be compatible with A. By /3.1 a function type
includes the return type. /5 does not list a placeholder as a non-deduced context.
5. Case `a`: P = `auto(T)` against A = `void(U*)` fails on `auto` against `void`.
P = `void(T*)` against A = `auto(U)` fails on `T*` against `U`. Neither template is at least
as specialized as the other ([temp.deduct.partial]/10), so the address is ambiguous.
[temp.deduct.funcaddr]/2 makes a placeholder return type a non-deduced context. That paragraph
covers deduction against the target type, not partial ordering. If partial ordering applied
it to P and A alike, case `b` would select `auto f(T *)` and case `c` would stay ambiguous.
Clang's results match neither reading. The [dcl.spec.auto] example, where both templates
return `auto`, is not affected.
### Versions
Accepts cases `a` and `c` with Ubuntu clang 18.1.8, 19.1.7, 20.1.8, 21.1.8 and 22.1.2
(x86-64 Linux, `-std=c++20`), and with clang 22.1.2 at `-std=c++14` and `-std=c++23`.
GCC 15.2.0 rejects both. Not checked on trunk.
### Analysis
In `clang/lib/Sema/SemaTemplateDeduction.cpp` (llvmorg-22.1.2), `isAtLeastAsSpecializedAs`
deduces the return types for `TPOC_Other` with `DeduceTemplateArgumentsByTypeMatch`
(around line 5676). There, `case Type::Auto` (around line 1875) returns `Success` when P is a
dependent placeholder, and cites [temp.deduct.funcaddr]. When A holds the placeholder and P is
`void`, P is not dependent, so the type comparison (around line 1847) fails. llvmorg-18.1.8
has the same code. Our reading: the placeholder rule belongs to deduction against the target
type. Partial ordering should compare a placeholder in P with A like any other type.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs