https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124910

            Bug ID: 124910
           Summary: [16 Regression] bogus 'std::optional{...}' is not a
                    constant expression error after resetting it via '=
                    nullopt'
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ppalka at gcc dot gnu.org
  Target Milestone: ---

$ cat testcase.C
struct nullopt_t { } nullopt;
using _Stored_type = int;
struct _Empty { };
struct _Optional_payload_base {
  union {
    _Stored_type _M_value = 1;
    _Empty _M_empty;
  };
  constexpr void _M_reset() {
    _M_value.~_Stored_type();
    _M_empty = {};
  }
};
struct _Optional_payload : _Optional_payload_base {};
struct _Optional_base {
  _Optional_payload _M_payload;
};
struct optional : _Optional_base {
  constexpr void operator=(nullopt_t) { _M_payload._M_reset(); }
};
template <typename, auto>
consteval void try_accumulate_element(optional &result) {
  result = nullopt;
}
template <auto Ms> struct unit_magnitude {
  template <typename> friend consteval optional try_get_value(unit_magnitude) {
    optional result;
    result = nullopt;
    return result;
  }
};
struct canonical_unit {
  unit_magnitude<0> mag;
} __trans_tmp_10;
template <class U> auto get_canonical_unit_result =
get_canonical_unit_impl(U{});
auto get_canonical_unit(auto u) {
  return get_canonical_unit_result<decltype(u)>;
}
struct derived_unit {};
template <class T> auto get_canonical_unit_impl(T) {
  auto num = __trans_tmp_10;
  return num;
}
template <class UFrom> void scaling_overflows_non_zero_values() {
  auto __trans_tmp_9(get_canonical_unit(UFrom{}).mag);
  try_get_value<long>(__trans_tmp_9);
}
constexpr auto v = scaling_overflows_non_zero_values<derived_unit>;

$ g++-15 -std=c++20 testcase.C
$ g++-16 -std=c++20 testcase.C
testcase.C: In instantiation of ‘void scaling_overflows_non_zero_values() [with
UFrom = derived_unit]’:
quantity_test.ii:48:20:   required from here
   48 | constexpr auto v = scaling_overflows_non_zero_values<derived_unit>;
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
testcase.C:46:22: error: call to consteval function ‘try_get_value<long
int>((__trans_tmp_9, unit_magnitude<0>()))’ is not a constant expression
   46 |   try_get_value<long>(__trans_tmp_9);
      |   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
testcase.C:47:1: error:
‘optional{_Optional_base{_Optional_payload{_Optional_payload_base{_Optional_payload_base::<unnamed
union>{(void)0}}}}}’ is not a constant expression
   47 | }
      | ^

Reply via email to