https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129181
Bug ID: 129181
Summary: Accepts incompatible `const` placeholder deduction
with `constexpr decltype(auto) a = f(), b = g();`
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: davidfromonline at gmail dot com
Target Milestone: ---
In an init-declarator-list that uses a placeholder type and has multiple
init-declarators, the placeholder must deduce to the same type for all
init-declarators (https://eel.is/c++draft/dcl.spec.auto#general-9.sentence-2).
For `decltype(auto)`, each initializer must have exactly the same type,
including cv qualifiers, because the placeholder type is the equivalent of
`decltype(initializer)`
(https://eel.is/c++draft/dcl.spec.auto#dcl.type.auto.deduct-4). However, when
combined with `constexpr`, each type gets an implicit `const`. gcc acts as
though that `const` "consumes" a `const` from the placeholder, meaning the
placeholder type does not contain `const` (as in `auto const &`). In other
words, the following ill-formed example is accepted by gcc:
```c++
struct s {};
constexpr auto pr() -> s {
return {};
}
constexpr auto cpr() -> s const {
return {};
}
constexpr decltype(auto) a = pr(), b = cpr();
```
See it live: See it live: https://godbolt.org/z/P8bxj5E8e
It should be rejected because it deduces `s` for `a` and `s const` for `b`, and
the standard does not have an exception saying that it's fine because it ends
up deducing the same type overall thanks to the implicit `const`.