https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108158
--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> --- Candidate fix: diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index be99bec17e7..6718c29b0cd 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -4301,9 +4301,13 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, if (!SCALAR_TYPE_P (elem_type)) { new_ctx = *ctx; - if (ctx->object) + if (ctx->object + && !same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (t), TREE_TYPE (ctx->object))) /* If there was no object, don't add one: it could confuse us - into thinking we're modifying a const object. */ + into thinking we're modifying a const object. Similarly, if + the types are the same, replacing .object could lead to a + failure to evaluate it (c++/108158). */ new_ctx.object = t; new_ctx.ctor = build_constructor (elem_type, NULL); ctx = &new_ctx;