1. Changelog
-
R5 (during Búzios):
-
Strike R4’s addition of a (wrong) example to [class.spaceship]. The example demonstrated R3 behavior, not R4 behavior; R4 made it simply ill-formed, exactly like the [class.eq] example, so the example was redundant.
-
Update § 3.3 "Deducing this" and CWG2586 since P2953 was adopted for C++29.
-
Strike "that is not defined as deleted" from the new wording in § 6.2 [class.spaceship]. This has the benefit of making
int operator<=>(const X&) = default;invariably ill-formed. In R4 it was ill-formed only if non-deleted. (Arthur thought R4 had achieved this, but it had not.)
-
-
R4 (pre-Búzios):
-
Add two examples for clarity.
-
Make
auto* operator++(int) = default;invariably ill-formed, rather than "ill-formed if non-deleted, well-formed if deleted." This harmonizes the wordings added to [class.copy.assign] and [over.inc.default]. Adjust § 3.2 "Defaulted as deleted" and § 3.7 Impact on existing code to match. -
Make
auto operator<=>(const X&) const = default;deducestrong_orderingif it is defaulted-as-deleted, rather than remaining undeduced. This has the benefit of makingauto* operator<=>(const X&) const = default;invariably ill-formed. In R3 it was ill-formed only if non-deleted.
-
-
R3 (pre-Búzios):
-
Wording changes after CWG review. Add the Annex C example.
-
Change paper title to be more descriptive of the full scope of the paper.
-
Update divergence table in § 3.6 Existing corner cases; list compiler versions tested.
-
-
R2 (pre-Hagenberg):
-
Forward to CWG (see § 5 Straw poll results).
-
Fix one mistake in a drafting note ("deleted" to "ill-formed").
-
-
R1:
-
Add discussion of
void operator=(X&&) = default, and of triviality, in § 3.5 Other return types are still forbidden.
-
2. Motivation
Current C++ permits =default to appear only on certain signatures, with certain return types.
The current wording prohibits the use of placeholder types such as auto& to express these return
types, with the single exception of C++20’s operator<=>. This leads to redundant repetition,
such as in this real code from libc++'s test suite:
| Today | After P2952 |
|---|---|
struct ForwardDiffView {
[...]
ForwardDiffView(ForwardDiffView&&) = default;
ForwardDiffView(const ForwardDiffView&) = default;
ForwardDiffView& operator=(ForwardDiffView&&) = default;
ForwardDiffView& operator=(const ForwardDiffView&) = default;
[...]
};
|
struct ForwardDiffView {
[...]
ForwardDiffView(ForwardDiffView&&) = default;
ForwardDiffView(const ForwardDiffView&) = default;
auto& operator=(ForwardDiffView&&) = default;
auto& operator=(const ForwardDiffView&) = default;
[...]
};
|
The comparison operators are inconsistent among themselves: operator<=>
can deduce strong_ordering, but the others cannot deduce bool.
| Today | After P2952 |
|---|---|
auto operator<=>(const MyClass& rhs) const = default; bool operator==(const MyClass& rhs) const = default; bool operator<(const MyClass& rhs) const = default; |
auto operator<=>(const MyClass& rhs) const = default; auto operator==(const MyClass& rhs) const = default; auto operator<(const MyClass& rhs) const = default; |
The status quo is inconsistent between non-defaulted and defaulted functions,
making it unnecessarily tedious to upgrade to =default:
| Today | After P2952 |
|---|---|
|
auto& operator=(const MyClass& r) |
|
auto operator==(const MyClass& r) const |
|
auto operator++(int) |
The ill-formedness of these declarations comes from overly restrictive wording in the standard,
such as [class.eq]/1
specifically requiring that a defaulted equality
operator must have a declared return type of bool, instead of simply specifying that its return type
must be bool.
We believe each of the examples above has an intuitively clear meaning: the placeholder return type
correctly matches the type which the defaulted body will actually return. We propose to loosen the
current restrictions and permit these declarations to be well-formed.
This proposal does not seek to change the set of valid return types for these functions.
We propose a purely syntactic change to expand the range of allowed declaration syntax, not semantics.
(But we do one drive-by clarification which we believe matches EWG’s original intent:
if an empty class’s defaulted operator<=> returns a non-comparison-category type,
it should be defaulted as deleted.)
3. Proposal
We propose that a defaulted function declaration with a placeholder return type should have its type deduced ([dcl.spec.auto.general]) as if from a fictional return statement that returns:
-
a prvalue of type
bool, in the case ofoperator==,operator!=,operator<,operator>,operator<=, oroperator>= -
a prvalue of type
Q, in the case ofoperator<=>; whereQis the common comparison type of R0, R1,... Rn-1 -
an lvalue of type
C, in the case ofoperator=for a class typeC -
an rvalue of type
C, in the case ofoperator++oroperator--for a class typeC
Then, the deduced return type is compared to the return type(s) permitted by the standard. If the types match, the declaration is well-formed. Otherwise it’s ill-formed.
struct MyClass {
auto& operator=(const MyClass&) = default; // Proposed OK: deduces MyClass&
decltype(auto) operator=(const MyClass&) = default; // Proposed OK: deduces MyClass&
auto&& operator=(const MyClass&) = default; // Proposed OK: deduces MyClass&
const auto& operator=(const MyClass&) = default; // Still ill-formed: deduced const MyClass& is not MyClass&
auto operator=(const MyClass&) = default; // Still ill-formed: deduced MyClass is not MyClass&
auto* operator=(const MyClass&) = default; // Still ill-formed: deduction fails
void operator=(const MyClass&) = default; // Still ill-formed: void is not MyClass&
};
For operator==, our proposal gives the following behavior:
struct MyClass {
auto operator==(const MyClass&) const = default; // Proposed OK: deduces bool
decltype(auto) operator==(const MyClass&) const = default; // Proposed OK: deduces bool
auto&& operator==(const MyClass&) const = default; // Still ill-formed: deduced bool&& is not bool
auto& operator==(const MyClass&) const = default; // Still ill-formed: deduction fails
};
3.1. "Return type" versus "declared return type"
Today, vendors unanimously reject auto& operator=(const A&) = default.
But we can’t find any wording in [class.copy.assign]
or [dcl.fct.def.default] that directly justifies
this behavior. It seems that vendors are interpreting e.g. [dcl.fct.def.default]/2.5’s
"[if] the return type of F1 differs from the return type of F2"
to mean "the declared return type of F1," even though newer sections such as
[class.compare] consistently distinguish
the "declared return type" from the (actual) return type.
We tentatively propose to leave [dcl.fct.def.default] alone, and simply add an example that indicates the (new) intent of the (existing) wording: that it should now be interpreted as talking about the assignment operator’s actual return type, not its declared (placeholder) return type.
3.2. "Defaulted as deleted"
The current wording for comparison operators is crafted so that the following Container is well-formed.
Its operator<=> is defaulted as deleted (so that operator is unusable), but the instantiation
of class Container<mutex> itself is OK. We need to preserve this in our rewriting.
(Godbolt.)
template<class T>
struct Container {
T t;
auto operator<=>(const Container&) const = default;
};
Container<std::mutex> cm;
// OK, <=> is deleted
struct Weird { int operator<=>(Weird) const; };
Container<Weird> cw;
// OK, <=> is deleted because Weird's operator<=>
// returns a non-comparison-category type
Here, [CWG3207] is relevant. Container<mutex>::operator<=> is deleted because mutex
is not comparable. We propose that the return type of the deleted operator<=> should be
strong_ordering. This means the return type of operator<=> is still a comparison category
type, so that Container<mutex> is not ill-formed.
Until R3, we considered the following case to be similar:
template<class R>
struct C {
int i;
R operator<=>(const C&) const = default;
};
static_assert(std::three_way_comparable<C<std::strong_ordering>>);
static_assert(!std::three_way_comparable<C<int>>);
// Today: OK, C<int>'s operator<=> is deleted
// Proposed: ill-formed, a defaulted operator<=> can't have return type int
But now this use of template parameter R as the return type of operator<=>, without forcing R
to be a comparison category type at all, strikes us as contrived and thus not necessary to support.
We aim particularly to make this simplest case ill-formed, instead of deleted:
struct C {
int operator<=>(const C&) const = default;
// Today: OK, defined as deleted
// Proposed: ill-formed, a defaulted operator<=> can't have return type int
};
3.3. "Deducing this" and CWG2586
[CWG2586] (adopted for C++23) permits defaulted functions to have explicit object parameters.
This constrained the wording we could choose for operator=: we couldn’t say “the return type is deduced
as if from return *this” because there might not be a *this.
There’s a quirk with rvalue-ref-qualified assignment operators — not move assignment, but assignment where the destination object is explicitly rvalue-ref-qualified.
-
auto&& operator=(const B&) && { return *this; }deduces an lvalue reference -
auto&& operator=(this B&& self, const B&) { return self; }deduces an rvalue reference
Nonetheless, a defaulted assignment operator always returns an lvalue reference ([class.copy.assign]/6, [dcl.fct.def.default]/2.5), regardless of whether it’s declared using explicit object syntax. In fact, C++29 has adopted our [P2953], which forbids them entirely. So P2952 doesn’t need to treat them specially at all.
struct A {
A& operator=(const A&) && = default; // Ill-formed since P2953
A&& operator=(const A&) && = default; // Ill-formed, return type isn't A&
decltype(auto) operator=(const A&) && { return *this; } // OK, deduces A&
decltype(auto) operator=(const A&) && = default; // Ill-formed since P2953; would have deduced A&
};
struct B {
B& operator=(this B&& self, const B&) { return self; } // Error, self can't bind to B&
B&& operator=(this B&& self, const B&) { return self; } // OK
decltype(auto) operator=(this B&& self, const B&) { return self; } // OK, deduces B&&
B& operator=(this B& self, const B&) = default; // OK
B& operator=(this B&& self, const B&) = default; // Ill-formed since P2953
B&& operator=(this B&& self, const B&) = default; // Ill-formed, return type isn't B&
decltype(auto) operator=(this B&& self, const B&) = default; // Ill-formed since P2953; would have deduced B&
};
3.4. Burden on specifying new defaultable operators
We propose to leave [dcl.fct.def.default] alone and reinterpret its term "return type" to mean the actual return type, not the declared return type. This will, by default, permit the programmer to use placeholder return types on their defaulted operators. So there is a new burden on the specification of the defaultable operator, to specify exactly how return type deduction works for the implicitly defined operator.
operator++(int) defaultable in
the same way as a secondary comparison operator, by
adding wording to [over.inc].
P2952 needs to modify this wording (see § 6.5 [over.inc.default] below)
to specify exactly how return type deduction works for defaulted increment and decrement operators.
3.5. Other return types are still forbidden
Notice that returning any type other than X& from a defaulted X::operator= remains ill-formed.
For example, we don’t propose to accept:
struct X {
void operator=(X&&) = default; // still ill-formed
};
Any paper that did propose to permit defaulting void operator= would have to consider very carefully
whether to call that operator "trivial." Today,
is_trivially_move_assignable_v<T> is true only when
decltype(declval<T&>() = declval<T>()) is T&. It’s just barely conceivable that existing
user code might rely on that fact, and break when presented with a trivial assignment
operator that returned void instead. And certainly the return-by-copy X operator=(X&&) = default
shouldn’t be considered "trivial" for non-trivially-copy-constructible X.
But none of this is a problem for this proposal P2952, because we propose to continue rejecting
a defaulted X::operator= that returns any type but X&. We merely propose to allow the programmer
to spell that return type using a placeholder such as auto&.
3.6. Existing corner cases
There is vendor divergence in some corner cases. Here is a table of the divergences we found, plus our opinion as to the conforming behavior, and our proposed behavior. The compilers tested are GCC 16, Clang 23, MSVS 18.6, and EDG 6.9.
| URL | Code | GCC | Clang | MSVC | EDG | Correct |
|---|---|---|---|---|---|---|
| link |
const bool operator==(const C&) const = default; | ✗ | ✗ | ✗ | ✓ | ✗ |
| link |
friend bool operator==(const C, const C) = default; | ✓ | ✗ | ✓ | ✓ | ✓? |
| link |
decltype(auto) operator<=>(const C&) const = default; | ✓ | ✗ | ✗ | ✗ | Today: ✗ Proposed: ✓ |
| link |
const auto operator<=>(const C&) const = default; | ✗ | ✗ | ✓ | ✓ | Today: ✗ Proposed: ✗ |
| link |
True auto operator<=>(const C&) const = default; | ✓ | ✗ | ✓ | ✓ | Today: ✗ Proposed: ✓ |
| link |
False auto operator<=>(const C&) const = default; | unmet | ✗ | ✓ | ✓ | Today: ✗ Proposed: unmet |
| link |
int operator<=>(const C&) const = default; | deleted | unusable | ✗ | deleted | Today: deleted? Proposed: ✗ |
| link |
struct U { U(strong_ordering); };
struct C {
U operator<=>(const C&) const = default;
};
| ✓ | ✓ | ✗ | deleted | Today: ✓ Proposed: ✗ |
| link |
struct U { U(strong_ordering); };
struct C {
int i;
U operator<=>(const C&) const = default;
};
| deleted | unusable | ✗ | deleted | Today: ? Proposed: ✗ |
| link |
struct U { U(strong_ordering); operator int(); };
struct C {
int i;
U operator<=>(const C&) const = default;
};
| ✓ | ✓ | ✗ | deleted | Today: ✓ Proposed: ✗ |
| link |
struct U { explicit U(strong_ordering); operator int(); };
struct C {
int i;
U operator<=>(const C&) const = default;
};
| deleted | ✓ | ✗ | deleted | Today: ✓ Proposed: ✗ |
| link |
struct C {
int i;
const strong_ordering&
operator<=>(const C&) const = default;
};
| ICE | ✓ | ✓ | deleted | deleted |
| link |
struct C {
const strong_ordering&
operator<=>(const C&) const = default;
};
| ✓ | ✓ | ✓ | deleted | Today: ✓ Proposed: ✗ |
| link |
auto operator<=>(const M&) const noexcept(false) = default; | ✓ | ✓ | noexcept | incon- sistent | ✓ |
| link |
C& operator=(C&) = default; | ✓ | ✓ | ✓ | ✓ | ✓ |
| link |
C& operator=(const C&&) = default; | deleted | deleted | ✗ | deleted | deleted |
| link |
C& operator=(const C&) const = default; | deleted | deleted | ✓ | deleted | deleted |
3.7. Impact on existing code
There should be little effect on existing code, since this proposal mainly allows syntax that was ill-formed before. As shown in § 3.6 Existing corner cases, we do propose to change some arcane and contrived examples, e.g.
struct U { U(std::strong_ordering); };
struct C {
U operator<=>(const C&) const = default;
// Today: Well-formed, non-deleted (GCC and Clang are correct)
// Tomorrow: Ill-formed (MSVC becomes correct)
};
struct D {
int i;
int operator<=>(const D&) const = default;
// Today: Well-formed, deleted (Clang and EDG are correct)
// Tomorrow: Ill-formed (MSVC becomes correct)
};
4. Implementation experience
None yet.
5. Straw poll results
Arthur O’Dwyer presented P2952R1 in the EWG telecon of 2025-01-08. The following straw polls were taken. The first was interpreted as "no consensus"; the second was interpreted as consensus (pending electronic polling).
| SF | F | N | A | SA | |
|---|---|---|---|---|---|
| EWG prefers this paper contains the change in P2953
(banning explicitly defaulted operator= with rvalue ref-qualifier). [Chair: This means EWG wants to see this paper again.] | 2 | 4 | 9 | 1 | 0 |
| Forward P2952R1 to CWG for inclusion in C++26, pending online polling. | 3 | 10 | 4 | 1 | 0 |
The electronic poll was taken as follows:
| SF | F | N | A | SA | |
|---|---|---|---|---|---|
| Forward P2952R1 to CWG for C++26 (pending confirmation during in-person meeting). | 3 | 6 | 1 | 1 | 0 |
This paper was presented again to EWG in Hagenberg. The following poll was taken:
| SF | F | N | A | SA | |
|---|---|---|---|---|---|
| P2952R2 auto& operator=(X&&) = default: forward to CWG for C++26. | 5 | 25 | 13 | 5 | 4 |
6. Proposed wording
6.1. [class.eq]
DRAFTING NOTE:
The phrase "equality operator function" ([over.binary])
means == or !=. But != is not covered by [class.eq]; it’s covered by [class.compare.secondary] below.
The new example is modeled on [dcl.spec.auto.general] Example 3.
Modify [class.eq] as follows:
1.
AIf the declared return type of a defaultedequality==operator function ([over.binary]) contains a placeholder type, its return type is deduced as if fromreturn true;. A defaulted==operator function shall havea declaredthe return typebool.2․ A defaulted
==operator function for a classCis defined as deleted unless, for eachxi in the expanded list of subobjects for an objectxof typeC,xi== xi is usable ([class.compare.default]).3․ The return value of a defaulted
==operator function with parametersxandyis determined by comparing corresponding elementsxi andyi in the expanded lists of subobjects forxandy(in increasing index order) until the first index i wherexi==yiyields a result value which,whencontextually converted tobool, yieldsfalse. The return value isfalseif such an index exists andtrueotherwise.4․ [Example 1:
struct D { int i; friend bool operator==(const D& x, const D& y) = default; // OK, returns x.i == y.i }; struct E { struct N {} n; auto* operator==(const E&) const = default; // error: cannot deduce auto* from true };— end example]
6.2. [class.spaceship]
DRAFTING NOTE:
There are only three "comparison category types" in C++, and strong_ordering::equal is implicitly convertible
to all three of them. The status quo already effectively forbids <=> to return a non-comparison-category type,
since either R is deduced as a common comparison type (which is a comparison category type by definition), or else
a synthesized three-way comparison of type R must exist (which means R must be a comparison category type),
or else the sequence xi must be empty (in which case there are no restrictions on R except
that it be constructible from strong_ordering::equal). We strengthen the wording to directly mandate that the
return type be a comparison category type, even in the empty case.
DRAFTING NOTE:
The wording below replaces the Tentatively Ready fix for [CWG3207]. That fix had proposed that a defaulted-as-deleted
auto operator<=> should remain undeduced. We propose instead that it should deduce strong_ordering. This means
that the placeholder return type of a defaulted-as-deleted <=> function is deduced, just as the
placeholder return type of a defaulted-as-deleted =, ++, --, ==, or secondary comparison operator is deduced.
In short: auto* operator<=>(...) = default is never well-formed, even if it’s defaulted-as-deleted.
Modify [class.spaceship] as follows:
[...]
2․ Let
Rbe the declared return type of a defaulted three-way comparison operator function, and letxi be the elements of the expanded list of subobjects for an objectxof typeC.
— (2.1)IfRiscontains a placeholder type , then let cviautoRi be the type of the expressionxi<=> xi.The operator function is defined as deleted if that expression is not usable or ifIf that expression is usable andRi is not a comparison category type ([cmp.categories.pre]) for any i.Ri is a comparison category type for all i, letQbe the common comparison type (see below) ofR0,R1, ...,Rn−1. Otherwise, letQbestd::strong_ordering. The return type is deduced as if fromreturn Q(std::strong_ordering::equal);the common comparison type (see below) of.R0,R1, ...,Rn-1x. Let
Sbe the (possibly deduced) return type of the defaulted three-way comparison operator function.Sshall be a comparison category type ([cmp.categories.pre]).x․
— (2.2) Otherwise,If the synthesized three-way comparison of typeRshall not contain a placeholder type.RSbetween any objectsxi andxi is not defined, the operator function is defined as deleted.3․ The return value
of typeof the defaulted three-way comparison operator function with parametersRxandyof the same typeis determined by comparing corresponding elementsxi andyi in the expanded lists of subobjects forxandy(in increasing index order) until the first index i where the synthesized three-way comparison of typeRSbetweenxi andyi yields a result valuevi wherevi!= 0, contextually converted tobool, yieldstrue. The return value isa copy ofvi if such an index exists andstatic_cast<otherwise.RS>(std::strong_ordering::equal)4․ The common comparison type
Uof a possibly-empty list of n comparison category typesT0,T1, ...,Tn-1 is defined as follows: [...]
6.3. [class.compare.secondary]
Modify [class.compare.secondary] as follows:
1․ A secondary comparison operator
@is a relational operator ([expr.rel]) or the!=operator.A defaulted operator function ([over.binary]) for a secondary comparison operator@shall have a declared return typebool.x. If the declared return type of a defaulted secondary comparison operator function contains a placeholder type, its return type is deduced as if from
return true;. A defaulted secondary comparison operator function shall have the return typebool.2․
TheA defaulted secondary comparison operator function with parametersxandyis defined as deleted if— (2.1) a first overload resolution ([over.match]), as applied to
x @ y,— (2.1.1) does not result in a usable candidate, or
— (2.1.2) the selected candidate is not a rewritten candidate, or
— (2.2) a second overload resolution for the expression resulting from the interpretation of
x @ yusing the selected rewritten candidate ([over.match.oper]) does not result in a usable candidate (for example, that expression might be(x <=> y) @ 0), or— (2.3)
x @ ycannot be implicitly converted tobool.In any of the two overload resolutions above, the defaulted operator function is not considered as a candidate for the
@operator. Otherwise, the operator function yieldsx @ y.3․ [Example 1:
struct HasNoLessThan { }; struct C { friend HasNoLessThan operator<=>(const C&, const C&); bool operator<(const C&) const = default; // OK, function is deleted };— end example]
6.4. [class.copy.assign]
DRAFTING NOTE:
[class.copy.assign]/6
already clearly states that "The implicitly-declared copy/move assignment operator for class X has the return type X&."
But we need this new wording to ensure that an explicitly-defaulted copy/move assignment operator
will deduce that same type. (If it deduces a different type, then the explicitly-defaulted operator
becomes ill-formed, as in example B below.)
DRAFTING NOTE:
We deduce from "an lvalue of type X"
to ensure that decltype(auto) operator=(const X&) = default; deduces X&, not X.
Modify [class.copy.assign] as follows:
14․ The implicitly-defined copy/move assignment operator for a class returns the object for which the assignment operator is invoked, that is, the object assigned to.
15․ If the declared return type of a defaulted copy/move assignment operator for a class
Xcontains a placeholder type, the return type is deduced as if fromreturn r;, whereris an lvalue of typeX.16․ [Example:
struct A { decltype(auto) operator=(A&&) = default; // OK, return type is A& }; struct B { auto operator=(B&&) = default; // error: return type is B, which violates [dcl.fct.def.default] };—end example]
6.5. [over.inc.default]
DRAFTING NOTE:
We deduce from "a prvalue of type C"
(rather than merely "an lvalue reference to C" or "a move-eligible rvalue reference to C")
to ensure that decltype(auto) operator++(int) = default; deduces a non-reference type.
Such code is contrived, but should do the Right Thing rather than the Wrong Thing if we can help it.
Modify [over.inc.default] as follows:
x․ If the declared return type of a defaulted postfix increment or decrement operator for a type
Ccontains a placeholder type, the return type is deduced as if fromreturn r;, whereris a prvalue of typeC.1․ A defaulted postfix increment or decrement operator function for
a typeCshall be a non-template function that— (1.1) has a first parameter of type “reference to
C” or a first parameter of type “reference to volatileC”, where the implicit object parameter (if any) is considered to be the first parameter,— (1.2) is defined as defaulted in
Cor in a context whereCis complete, and— (1.3) has
a declaredthe return typeofC.[Note 1: A postfix increment or decrement operator always has a parameter of type int ([over.inc.general]). — end note]
Name lookups and access checks in the implicit definition ([dcl.fct.def.default]) of a defaulted postfix increment or decrement operator function are performed from a context equivalent to its function-body. A definition of a postfix increment or decrement operator as defaulted that appears in a class shall be the first declaration of that function.
[Example 1:
struct S; S operator++(S&, int) = default; // error: S is not complete— end example]2․ A defaulted postfix increment or decrement operator function for a type
Cis defined as deleted if [...]
6.6. Annex C, [diff.cpp26]
Add a clause to Annex C, [diff.cpp26.class], as follows:
Affected clause: [class.spaceship]
Change: It is invalid for a defaulted three-way comparison operator of an empty class type to return a type other than a comparison category type.
Rationale: Consistency with the existing restriction on defaulted three-way comparison operators of non-empty class types.
Effect on original feature: A valid C++26 program that contains a defaulted three-way comparison operator with a return type that is not a comparison category type is ill-formed.
Example:struct U { U(std::strong_ordering); }; struct C { U operator<=>(const C&) const = default; // ill-formed; previously well-formed };