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

            Bug ID: 107598
           Summary: implicitly-defined copy/move assignment op not
                    constexpr
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpolacek at gcc dot gnu.org
  Target Milestone: ---

The following test is rejected with:

bug1.C: In function ‘constexpr void g()’:
bug1.C:11:7: error: call to non-‘constexpr’ function ‘S& S::operator=(const
S&)’
   11 |   b = a;
      |       ^
bug1.C:5:4: note: ‘S& S::operator=(const S&)’ is not usable as a ‘constexpr’
function because:
    5 | S& S::operator=(const S&) = default;
      |    ^

...because what?

I think it should be accepted, at least in C++23, because
[class.copy.assign]/10:
"A copy/move assignment operator for a class X that is defaulted and not
defined as deleted is implicitly defined when it is odr-used ([basic.def.odr])
(e.g., when it is selected by overload resolution to assign to an object of its
class type), when it is needed for constant evaluation ([expr.const]), or when
it is explicitly defaulted after its first declaration.
The implicitly-defined copy/move assignment operator is constexpr."

Here it is explicitly defaulted after its first declaration.


struct S {
  constexpr S() {}
  S& operator=(const S&);
};
S& S::operator=(const S&) = default;

constexpr void
g ()
{
  S a, b;
  b = a;
}

Reply via email to