return false;
}
- if (!ctx)
- {
- if (DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR))
- error_at (loc, "three-way comparison operator can only be defaulted "
- "in a class definition");
- else
- error_at (loc, "equality comparison operator can only be defaulted "
- "in a class definition");
- return false;
- }
-
if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
&& !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node))
{
for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
{
tree parmtype = TREE_VALUE (parmnode);
- if (same_type_p (parmtype, ctx))
+ if (CLASS_TYPE_P (parmtype))
saw_byval = true;
- else if (TREE_CODE (parmtype) != REFERENCE_TYPE
- || TYPE_REF_IS_RVALUE (parmtype)
- || TYPE_QUALS (TREE_TYPE (parmtype)) != TYPE_QUAL_CONST
- || !(same_type_ignoring_top_level_qualifiers_p
- (TREE_TYPE (parmtype), ctx)))
- saw_bad = true;
+ else if (TREE_CODE (parmtype) == REFERENCE_TYPE
+ && !TYPE_REF_IS_RVALUE (parmtype)
+ && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
+ {
+ saw_byref = true;
+ parmtype = TREE_TYPE (parmtype);
+ }
else
- saw_byref = true;
+ saw_bad = true;
+
+ if (!saw_bad && !ctx)
+ {
+ /* Defaulted outside the class body. */
+ ctx = TYPE_MAIN_VARIANT (parmtype);
+ if (!is_friend (ctx, fn))
+ error_at (loc, "defaulted %qD is not a friend of %qT", fn, ctx);
+ }
+ else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
+ saw_bad = true;
}
if (saw_bad || (saw_byval && saw_byref))
// { dg-do compile { target c++20 } }
struct B {};
-bool operator!=(const B&, const B&) = default; // { dg-error "equality comparison operator can only be defaulted in a class definition" }
-bool operator==(const B&, const B&) = default; // { dg-error "equality comparison operator can only be defaulted in a class definition" }
-bool operator<=>(const B&, const B&) = default; // { dg-error "three-way comparison operator can only be defaulted in a class definition" }
+bool operator!=(const B&, const B&) = default; // { dg-error "not a friend" }
+bool operator==(const B&, const B&) = default; // { dg-error "not a friend" }
+bool operator<=>(const B&, const B&) = default; // { dg-error "not a friend" }
--- /dev/null
+// P2085, separate definition of defaulted comparisons
+// { dg-do compile { target c++20 } }
+
+namespace X {
+
+ struct A {
+ int i;
+ friend constexpr bool operator==(const A&,const A&);
+ };
+
+ inline constexpr bool operator==(const A&,const A&)=default;
+
+ static_assert (A() == A());
+
+}
+
+namespace Y {
+
+ struct A {
+ int i;
+ // friend bool operator==(const A&,const A&);
+ };
+
+ inline bool operator==(const A&,const A&)=default; // { dg-error "not a friend" }
+
+}