https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120324
Bug ID: 120324
Summary: Code rejected as constant due to invalid read through
reference member
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: barry.revzin at gmail dot com
Target Milestone: ---
This might actually be a CWG issue rather than a GCC one, as Clang rejects for
the same reason, but I thought I'd post here first just in case.
Here is an example that somebody sent me, which I reduced/restructured a bit:
struct A3 {
constexpr auto size() const -> int { return 3; }
};
template <typename T>
struct wrap {
T target;
constexpr auto size1() const -> int { return target.size(); }
friend constexpr auto size2(wrap const& w) -> int { return w.target.size();
}
constexpr auto size3(this wrap const& w) -> int { return w.target.size(); }
};
void test(A3 const& a) {
static_assert(a.size() == 3); // ok
auto w0 = wrap<A3>{a};
static_assert(w0.size1() == 3); // ok
static_assert(size2(w0) == 3); // ok
static_assert(w0.size3() == 3); // ok
auto w1 = wrap<A3 const>{a};
static_assert(w1.size1() == 3); // ok
static_assert(size2(w1) == 3); // ok
static_assert(w1.size3() == 3); // ok
auto w2 = wrap<A3 const&>{a};
static_assert(w2.size1() == 3); // error
static_assert(size2(w2) == 3); // error
static_assert(w2.size3() == 3); // error
}
Both gcc and clang reject the last set of assertions (wrote it every
interesting way, just in case) for trying to read through w2.target as not
being constexpr. But w2.target is an A3 const&, just like a is. And the intent
of the proposal was to allow this
(https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2280r4.html#the-this-pointer)
— after all we never have to read *through* the reference, we're basically just
operating on its type.
Why is this failing? Are we still missing wording to allow this or is this a
gcc/clang bug to reject?