[clang] Clang accepts explicit instantiation of a function template with deduced return type and explicit trailing return type (non‑standard)
I suspect that Clang currently accepts a non‑standard form of explicit instantiation that other major compilers (GCC, MSVC, EDG) correctly reject:
struct A {
char g() { return 'a'; }
template<class T> auto f(T t) {
return t + g();
}
};
template auto A::f<int>(int t) -> decltype(t + g());
// The correct code should probably be:
// template auto A::f<int>(int t);
https://godbolt.org/z/cdz4Y3Tea
What’s puzzling is the version history:
- Clang ≤ 16.0.0 correctly rejected this code.
- Clang 17.0.1 – 18.1.0 caused a crash.
- Clang 19.1.0+ now accepts the code silently.
It would be helpful to know which change or patch introduced this behavior,
and whether the current acceptance is intentional or a missing diagnostic.
@llvm/issue-subscribers-clang-frontend
Author: None (Attacker23)
struct A {
char g() { return 'a'; }
template<class T> auto f(T t) {
return t + g();
}
};
template auto A::f<int>(int t) -> decltype(t + g());
// The correct code should probably be:
// template auto A::f<int>(int t);
https://godbolt.org/z/cdz4Y3Tea
What’s puzzling is the version history:
- Clang ≤ 16.0.0 correctly rejected this code.
- Clang 17.0.1 – 18.1.0 caused a crash.
- Clang 19.1.0+ now accepts the code silently.
It would be helpful to know which change or patch introduced this behavior,
and whether the current acceptance is intentional or a missing diagnostic.
Local bisection: Silently accepts the explicit instantiation → Diagnoses the explicit instantiation at
https://github.com/llvm/llvm-project/commit/d2f75f2fe3261264bb4692368aab64aaafb30f08
(f1bf0b02ae25 → d2f75f2fe326).
CE comparison: Clang 21.1.0 (assertions) → Clang 22.1.0 (assertions), showing the same transition.
Current CE trunk: Diagnoses the same construct with invalid use of 'this' outside of a non-static member function.
This reports the observed behavior, without asserting which behavior is required.
Prepared with assistance from Codex.
4. Local bisection: Silently accepts the explicit instantiation → Diagnoses the explicit instantiation at d2f75f2 (
f1bf0b02ae25→d2f75f2fe326). CE comparison: Clang 21.1.0 (assertions) → Clang 22.1.0 (assertions), showing the same transition. Current CE trunk: Diagnoses the same construct withinvalid use of 'this' outside of a non-static member function. This reports the observed behavior, without asserting which behavior is required. Prepared with assistance from Codex.
@shafik Thanks a lot!
@mizvekov