This is a bit of a work in progress. It still needs proper
diagnostics, but for the most part everything is fairly simple. I
noticed there are some bugs allowing for incorrect behavior when a
function is defaulted after another declaration, this occurs for other
defaulted functions as well.
Alex
From 5dcd8112d96221c24fad4a76d735871ec4ca8aa5 Mon Sep 17 00:00:00 2001
From: Waffl3x <[email protected]>
Date: Mon, 16 Nov 2026 04:53:11 -0600
Subject: [PATCH] C++: P3668 defaulting postfix increment and decerement
Adds support for P3668. There are bugs allowing for some disallowed cases
but they are not new, these were already present and effect defaulted
comparison operator as well.
gcc/cp/ChangeLog:
* cp-tree.h (enum special_function_kind): Add sfk_postinc_postdec.
* method.cc (type_has_trivial_fn): Handle sfk_postinc_postdec.
(build_default_postinc_postdec_op): New.
(early_check_defaulted_postinc_postdec): New.
(synthesize_method): Handle sfk_postinc_postdec case.
(maybe_synthesize_method): Handle sfk_postinc_postdec case.
(maybe_explain_implicit_delete): Handle sfk_postinc_postdec case.
(defaulted_late_check): Handle sfk_postinc_postdec case.
(defaultable_fn_check): Handle sfk_postinc_postdec case.
* parser.cc (cp_parser_pure_specifier): Update comment.
* tree.cc (special_function_p): Add sfk_postinc_postdec case.
gcc/testsuite/ChangeLog:
* g++.dg/cpp29/defaulted-postfix-inc-dec1.C: New test.
* g++.dg/cpp29/defaulted-postfix-inc-dec2.C: New test.
---
gcc/cp/cp-tree.h | 1 +
gcc/cp/method.cc | 188 +++++++++-
gcc/cp/parser.cc | 16 +-
gcc/cp/tree.cc | 3 +
.../g++.dg/cpp29/defaulted-postfix-inc-dec1.C | 321 ++++++++++++++++++
.../g++.dg/cpp29/defaulted-postfix-inc-dec2.C | 217 ++++++++++++
6 files changed, 742 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec1.C
create mode 100644 gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec2.C
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 8c6d0ea3744..d5dd3e60e60 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6187,6 +6187,7 @@ enum special_function_kind {
sfk_conversion, /* A conversion operator. */
sfk_deduction_guide, /* A class template deduction guide. */
sfk_comparison, /* A comparison operator (e.g. ==, <, <=>). */
+ sfk_postinc_postdec, /* A postfix increment or decrement operator. */
sfk_virtual_destructor /* Used by member synthesis fns. */
};
diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
index 20c1462bb72..e4a5a714d3a 100644
--- a/gcc/cp/method.cc
+++ b/gcc/cp/method.cc
@@ -414,6 +414,7 @@ type_has_trivial_fn (tree ctype, special_function_kind sfk)
return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
case sfk_inheriting_constructor:
case sfk_comparison:
+ case sfk_postinc_postdec:
return false;
default:
gcc_unreachable ();
@@ -915,6 +916,161 @@ do_build_copy_assign (tree fndecl)
finish_compound_stmt (compound_stmt);
}
+static tree destructible_expr (tree to, bool explain);
+
+/* Modifies FN. */
+
+static void
+build_default_postinc_postdec_op (tree fn, tsubst_flags_t complain)
+{
+ gcc_checking_assert (!processing_template_decl);
+ gcc_checking_assert (DECL_OVERLOADED_OPERATOR_IS (fn, POSTINCREMENT_EXPR)
+ || DECL_OVERLOADED_OPERATOR_IS (fn,
+ POSTDECREMENT_EXPR));
+ /* At the moment, we don't keep track of whether a function has been declared
+ previously, this makes it impossible to do some things correctly.
+ For now, just always assume it is the first declaration. This does indeed
+ cause bugs.
+ The one exception seems to be if it were declared as a friend, if it were
+ then we know for sure it must have already been declared. This is hardly
+ a real solution but at least it prevents some cases of it. */
+ const bool first_declaration = DECL_DEFAULTED_IN_CLASS_P (fn)
+ || !DECL_BEFRIENDING_CLASSES (fn);
+
+ DECL_MAYBE_DELETED (fn) = false;
+ const tree obj_ref = DECL_IOBJ_MEMBER_FUNCTION_P (fn) && current_class_ref
+ ? current_class_ref
+ : cp_build_fold_indirect_ref (DECL_ARGUMENTS (fn));
+ const tree obj_type = strip_top_quals (TREE_TYPE (obj_ref));
+ gcc_checking_assert (COMPLETE_TYPE_P (obj_type));
+
+ /* Map to the corresponding increment expr code. */
+ const tree_code op_code
+ = DECL_OVERLOADED_OPERATOR_IS (fn, POSTINCREMENT_EXPR) ? PREINCREMENT_EXPR
+ : PREDECREMENT_EXPR;
+ const location_t loc = DECL_SOURCE_LOCATION (fn);
+
+ tree temp = create_temporary_var (obj_type);
+ tree init = cp_build_modify_expr (loc, temp, INIT_EXPR, obj_ref, complain);
+ tree op_expr = build_x_unary_op (loc, op_code, obj_ref, NULL_TREE, complain);
+
+ if (!init || error_operand_p (init)
+ || !op_expr || error_operand_p (op_expr)
+ /* Destructibility is not covered by the above expressions. */
+ || destructible_expr (obj_type, complain & tf_error) == error_mark_node)
+ {
+ DECL_DELETED_FN (fn) = true;
+ return;
+ }
+ gcc_checking_assert (!DECL_DELETED_FN (fn));
+
+ tree compound_stmt = begin_compound_stmt (0);
+
+ bool exprs_constexpr = true;
+ bool exprs_noexcept = true;
+ auto check_expr = [&] (tree expr)
+ {
+ exprs_constexpr = exprs_constexpr
+ && potential_rvalue_constant_expression (expr);
+ exprs_noexcept = exprs_noexcept && expr_noexcept_p (expr, tf_none);
+ };
+
+ /* FIXME: Does not correctly take into account a noexcept(false) on a default
+ move constructor. (And assumedly a copy constructor too). */
+
+ add_decl_expr (temp);
+ /* Do we need 'cp_finish_decl (temp)' here? I think it does much the same
+ things as add_decl_expr. */
+ check_expr (finish_expr_stmt (init));
+ check_expr (finish_expr_stmt (op_expr));
+ check_expr (finish_return_stmt (temp));
+
+ finish_compound_stmt (compound_stmt);
+
+ if (!first_declaration)
+ return;
+ DECL_DECLARED_INLINE_P (fn) = true;
+ /* Technically, in C++26 this is always constexpr as the only requirement
+ is that the function is constexpr-suitable, which only means not a
+ coroutine in C++26. However, if we want to pretend to support this as
+ an extension in C++26 and below, we should match the semantics. */
+ DECL_DECLARED_CONSTEXPR_P (fn) = cxx_dialect >= cxx26
+ || exprs_constexpr;
+
+ const tree declared_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
+ if (!declared_spec || UNEVALUATED_NOEXCEPT_SPEC_P (declared_spec))
+ {
+ const tree new_spec = exprs_noexcept ? noexcept_true_spec
+ : noexcept_false_spec;
+ TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), new_spec);
+ }
+}
+
+static bool
+early_check_defaulted_postinc_postdec (const_tree decl)
+{
+ gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL);
+
+ const auto get_first_parm_type = [] (const_tree decl)
+ {
+ gcc_checking_assert (TREE_CODE (TREE_TYPE (decl)) != METHOD_TYPE);
+ return TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)));
+ };
+
+ const tree type_op_is_for
+ = DECL_OBJECT_MEMBER_FUNCTION_P (decl)
+ ? DECL_CONTEXT (decl)
+ : strip_top_quals (non_reference (get_first_parm_type (decl)));
+ /* Should already be handled. */
+ gcc_checking_assert (OVERLOAD_TYPE_P (type_op_is_for));
+
+ /* TODO: Need to check if a defaulted func in a class was NOT already
+ declared. This is a bug that is already present. */
+
+ if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
+ && !COMPLETE_TYPE_P (type_op_is_for))
+ {
+ const tree class_friend_is_defined_in = DECL_FRIEND_CONTEXT (decl);
+ if (class_friend_is_defined_in == NULL_TREE
+ || !same_type_p (class_friend_is_defined_in, type_op_is_for))
+ return false;
+ }
+
+ /* Check object parameter type. */
+ if (!DECL_IOBJ_MEMBER_FUNCTION_P (decl))
+ {
+ const tree first_parm_type = get_first_parm_type (decl);
+ /* Unfortunately, volatile is allowed. */
+ if (!TYPE_REF_P (first_parm_type)
+ || CP_TYPE_CONST_P (TREE_TYPE (first_parm_type))
+ || (DECL_XOBJ_MEMBER_FUNCTION_P (decl)
+ && !same_type_p (type_op_is_for,
+ strip_top_quals (TREE_TYPE (first_parm_type)))))
+ return false;
+ }
+ else
+ {
+ if ((type_memfn_quals (TREE_TYPE (decl)) & TYPE_QUAL_CONST) != 0)
+ return false;
+ }
+
+ const tree return_type = TREE_TYPE (TREE_TYPE (decl));
+ if (is_auto (return_type))
+ return false;
+ /* Should be impossible. */
+ gcc_checking_assert (!FNDECL_USED_AUTO (decl));
+
+ if (!same_type_p (type_op_is_for, return_type))
+ return false;
+
+ /* The int parameter was already checked. */
+ gcc_checking_assert (TREE_VALUE (DECL_IOBJ_MEMBER_FUNCTION_P (decl)
+ ? FUNCTION_FIRST_USER_PARMTYPE (decl)
+ : TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))))
+ == integer_type_node);
+ return true;
+}
+
/* C++20 <compare> comparison category types. */
enum comp_cat_tag
@@ -1839,6 +1995,11 @@ synthesize_method (tree fndecl)
build_comparison_op (fndecl, true, tf_none);
need_body = false;
}
+ else if (sfk == sfk_postinc_postdec)
+ {
+ build_default_postinc_postdec_op (fndecl, tf_none);
+ need_body = false;
+ }
/* If we haven't yet generated the body of the function, just
generate an empty compound statement. */
@@ -1877,7 +2038,8 @@ synthesize_method (tree fndecl)
void
maybe_synthesize_method (tree fndecl)
{
- if (special_function_p (fndecl) == sfk_comparison)
+ const special_function_kind kind = special_function_p (fndecl);
+ if (kind == sfk_comparison || kind == sfk_postinc_postdec)
{
tree lhs = DECL_ARGUMENTS (fndecl);
if (is_this_parameter (lhs))
@@ -1887,6 +2049,8 @@ maybe_synthesize_method (tree fndecl)
tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
if (!COMPLETE_TYPE_P (ctype))
{
+ if (kind == sfk_postinc_postdec)
+ return;
push_deferring_access_checks (dk_no_deferred);
build_comparison_op (fndecl, false, tf_none);
pop_deferring_access_checks ();
@@ -3322,12 +3486,15 @@ maybe_explain_implicit_delete (tree decl)
informed = true;
}
}
- if (!informed && sfk == sfk_comparison)
+ if (!informed && (sfk == sfk_comparison || sfk == sfk_postinc_postdec))
{
inform (DECL_SOURCE_LOCATION (decl),
"%q#D is implicitly deleted because the default "
"definition would be ill-formed:", decl);
- build_comparison_op (decl, false, tf_warning_or_error);
+ if (sfk == sfk_comparison)
+ build_comparison_op (decl, false, tf_warning_or_error);
+ else
+ build_default_postinc_postdec_op (decl, tf_warning_or_error);
}
else if (!informed)
{
@@ -3907,6 +4074,8 @@ defaulted_late_check (tree fn, tristate imp_const/*=tristate::unknown()*/)
tree ctx = DECL_CONTEXT (fn);
special_function_kind kind = special_function_p (fn);
+ if (kind == sfk_postinc_postdec)
+ return; /* TODO. */
if (kind == sfk_comparison)
{
/* If the function was declared constexpr, check that the definition
@@ -4016,6 +4185,19 @@ defaultable_fn_check (tree fn)
if (!early_check_defaulted_comparison (fn))
return false;
}
+ else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) == OVL_OP_POSTINCREMENT_EXPR
+ || DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
+ == OVL_OP_POSTDECREMENT_EXPR)
+ {
+ kind = sfk_postinc_postdec;
+ if (!early_check_defaulted_postinc_postdec (fn))
+ {
+ error ("bad defaulted postfix");
+ return false;
+ }
+ if (CLASS_TYPE_P (DECL_CONTEXT (fn)))
+ DECL_MAYBE_DELETED (fn) = true;
+ }
/* FIXME: We need to check for xobj member functions here to give better
diagnostics for weird cases where unrelated xobj parameters are given.
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index f1e2207d8bd..777b0d827f2 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -31995,12 +31995,26 @@ cp_parser_member_declaration (cp_parser* parser)
cp_finalize_omp_declare_simd (parser, &odsd);
}
-/* Parse a pure-specifier.
+/* Parse a pure-specifier, a function-body of the form '= default ;', or a
+ deleted-function-body.
pure-specifier:
= 0
+ function-body:
+ = default ;
+ deleted-function-body
+
+ deleted-function-body:
+ = delete ;
+ = delete ( unevaluated-string ) ;
+
Returns INTEGER_ZERO_NODE if a pure specifier is found.
+
+ Otherwise, returns a 'delete' or 'default' IDENTIFIER_NODE, unless it is of
+ the form delete("reason") then a STRING_CST with the IDENTIFIER_NODE
+ smuggled through its TREE_TYPE field is returned.
+
Otherwise, ERROR_MARK_NODE is returned. */
static tree
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index d16fee9b0e9..6171d593eb3 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -6702,6 +6702,9 @@ special_function_p (const_tree decl)
if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
&& DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
return sfk_comparison;
+ if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) == OVL_OP_POSTINCREMENT_EXPR
+ || DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) == OVL_OP_POSTDECREMENT_EXPR)
+ return sfk_postinc_postdec;
return sfk_none;
}
diff --git a/gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec1.C b/gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec1.C
new file mode 100644
index 00000000000..dfb41d66004
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec1.C
@@ -0,0 +1,321 @@
+// P3668R4 - Defaulting Postfix Increment and Decrement Operations
+// { dg-do run { target c++11 } }
+
+struct iobj0
+{
+ int _v;
+
+ void operator++()
+ { ++_v; }
+ iobj0 operator++(int) = default;
+
+ void operator--()
+ { --_v; }
+ iobj0 operator--(int) = default;
+};
+
+struct iobj1
+{
+ int _v;
+
+ void operator++() &
+ { ++_v; }
+ iobj1 operator++(int) = default;
+
+ void operator--() &
+ { --_v; }
+ iobj1 operator--(int) = default;
+};
+
+struct iobj2
+{
+ int _v;
+
+ void operator++()
+ { ++_v; }
+ iobj2 operator++(int) & = default;
+
+ void operator--()
+ { --_v; }
+ iobj2 operator--(int) & = default;
+};
+
+struct iobj3
+{
+ int _v;
+
+ void operator++() &
+ { ++_v; }
+ iobj3 operator++(int) & = default;
+
+ void operator--() &
+ { --_v; }
+ iobj3 operator--(int) & = default;
+};
+
+struct freefn0
+{
+ int _v;
+
+ void operator++()
+ { ++_v; }
+
+ void operator--()
+ { --_v; }
+};
+
+freefn0 operator++(freefn0&, int) = default;
+freefn0 operator--(freefn0&, int) = default;
+
+
+struct freefn1
+{
+ int _v;
+
+ void operator++() &
+ { ++_v; }
+
+ void operator--() &
+ { --_v; }
+};
+
+freefn1 operator++(freefn1&, int) = default;
+freefn1 operator--(freefn1&, int) = default;
+
+
+struct freefn2
+{
+ int _v;
+};
+
+void operator++(freefn2& self)
+{ ++self._v; }
+void operator--(freefn2& self)
+{ --self._v; }
+
+freefn2 operator++(freefn2&, int) = default;
+freefn2 operator--(freefn2&, int) = default;
+
+
+struct free_iob0;
+void operator++(free_iob0&);
+void operator--(free_iob0&);
+
+struct free_iob0
+{
+ int _v;
+
+ free_iob0 operator++(int) = default;
+ free_iob0 operator--(int) = default;
+};
+
+void operator++(free_iob0& self)
+{ ++self._v; }
+void operator--(free_iob0& self)
+{ --self._v; }
+
+
+struct free_iob1;
+void operator++(free_iob1&);
+void operator--(free_iob1&);
+
+struct free_iob1
+{
+ int _v;
+
+ free_iob1 operator++(int) & = default;
+ free_iob1 operator--(int) & = default;
+};
+
+void operator++(free_iob1& self)
+{ ++self._v; }
+void operator--(free_iob1& self)
+{ --self._v; }
+
+
+struct friend0
+{
+ int _v;
+
+ void operator++()
+ { ++_v; }
+ void operator--()
+ { --_v; }
+
+ friend friend0 operator++(friend0&, int) = default;
+ friend friend0 operator--(friend0&, int) = default;
+};
+
+struct friend1
+{
+ int _v;
+
+ void operator++() &
+ { ++_v; }
+ void operator--() &
+ { --_v; }
+
+ friend friend1 operator++(friend1&, int) = default;
+ friend friend1 operator--(friend1&, int) = default;
+};
+
+struct friend2
+{
+ int _v;
+
+ friend void operator++(friend2& self)
+ { ++self._v; }
+ friend void operator--(friend2& self)
+ { --self._v; }
+
+ friend friend2 operator++(friend2&, int) = default;
+ friend friend2 operator--(friend2&, int) = default;
+};
+
+
+
+enum e0 { e0_a, e0_b };
+
+
+int main()
+{
+ iobj0 v_i0{42};
+ iobj1 v_i1{42};
+ iobj2 v_i2{42};
+ iobj3 v_i3{42};
+ freefn0 v_free0{42};
+ freefn1 v_free1{42};
+ freefn2 v_free2{42};
+ free_iob0 v_fi0{42};
+ free_iob1 v_fi1{42};
+ friend0 v_friend0{42};
+ friend1 v_friend1{42};
+ friend2 v_friend2{42};
+
+ // Sanity check.
+
+ if (v_i0._v != 42)
+ __builtin_abort ();
+ if (v_i1._v != 42)
+ __builtin_abort ();
+ if (v_i2._v != 42)
+ __builtin_abort ();
+ if (v_i3._v != 42)
+ __builtin_abort ();
+ if (v_free0._v != 42)
+ __builtin_abort ();
+ if (v_free1._v != 42)
+ __builtin_abort ();
+ if (v_free2._v != 42)
+ __builtin_abort ();
+ if (v_fi0._v != 42)
+ __builtin_abort ();
+ if (v_fi1._v != 42)
+ __builtin_abort ();
+ if (v_friend0._v != 42)
+ __builtin_abort ();
+ if (v_friend1._v != 42)
+ __builtin_abort ();
+ if (v_friend2._v != 42)
+ __builtin_abort ();
+
+ // Postfix increment.
+ if ((v_i0++)._v != 42)
+ __builtin_abort ();
+ if (v_i0._v != 43)
+ __builtin_abort ();
+ if ((v_i1++)._v != 42)
+ __builtin_abort ();
+ if (v_i1._v != 43)
+ __builtin_abort ();
+ if ((v_i2++)._v != 42)
+ __builtin_abort ();
+ if (v_i2._v != 43)
+ __builtin_abort ();
+ if ((v_i3++)._v != 42)
+ __builtin_abort ();
+ if (v_i3._v != 43)
+ __builtin_abort ();
+ if ((v_free0++)._v != 42)
+ __builtin_abort ();
+ if (v_free0._v != 43)
+ __builtin_abort ();
+ if ((v_free1++)._v != 42)
+ __builtin_abort ();
+ if (v_free1._v != 43)
+ __builtin_abort ();
+ if ((v_free2++)._v != 42)
+ __builtin_abort ();
+ if (v_free2._v != 43)
+ __builtin_abort ();
+ if ((v_fi0++)._v != 42)
+ __builtin_abort ();
+ if (v_fi0._v != 43)
+ __builtin_abort ();
+ if ((v_fi1++)._v != 42)
+ __builtin_abort ();
+ if (v_fi1._v != 43)
+ __builtin_abort ();
+ if ((v_friend0++)._v != 42)
+ __builtin_abort ();
+ if (v_friend0._v != 43)
+ __builtin_abort ();
+ if ((v_friend1++)._v != 42)
+ __builtin_abort ();
+ if (v_friend1._v != 43)
+ __builtin_abort ();
+ if ((v_friend2++)._v != 42)
+ __builtin_abort ();
+ if (v_friend2._v != 43)
+ __builtin_abort ();
+
+ // Postfix decrement.
+ if ((v_i0--)._v != 43)
+ __builtin_abort ();
+ if (v_i0._v != 42)
+ __builtin_abort ();
+ if ((v_i1--)._v != 43)
+ __builtin_abort ();
+ if (v_i1._v != 42)
+ __builtin_abort ();
+ if ((v_i2--)._v != 43)
+ __builtin_abort ();
+ if (v_i2._v != 42)
+ __builtin_abort ();
+ if ((v_i3--)._v != 43)
+ __builtin_abort ();
+ if (v_i3._v != 42)
+ __builtin_abort ();
+ if ((v_free0--)._v != 43)
+ __builtin_abort ();
+ if (v_free0._v != 42)
+ __builtin_abort ();
+ if ((v_free1--)._v != 43)
+ __builtin_abort ();
+ if (v_free1._v != 42)
+ __builtin_abort ();
+ if ((v_free2--)._v != 43)
+ __builtin_abort ();
+ if (v_free2._v != 42)
+ __builtin_abort ();
+ if ((v_fi0--)._v != 43)
+ __builtin_abort ();
+ if (v_fi0._v != 42)
+ __builtin_abort ();
+ if ((v_fi1--)._v != 43)
+ __builtin_abort ();
+ if (v_fi1._v != 42)
+ __builtin_abort ();
+ if ((v_friend0--)._v != 43)
+ __builtin_abort ();
+ if (v_friend0._v != 42)
+ __builtin_abort ();
+ if ((v_friend1--)._v != 43)
+ __builtin_abort ();
+ if (v_friend1._v != 42)
+ __builtin_abort ();
+ if ((v_friend2--)._v != 43)
+ __builtin_abort ();
+ if (v_friend2._v != 42)
+ __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec2.C b/gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec2.C
new file mode 100644
index 00000000000..26d46043a4f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp29/defaulted-postfix-inc-dec2.C
@@ -0,0 +1,217 @@
+// P3668R4 - Defaulting Postfix Increment and Decrement Operations
+// { dg-do run { target c++23 } }
+
+struct xobj_iobj0
+{
+ int _v;
+
+ void operator++(this xobj_iobj0& self)
+ { ++self._v; }
+ xobj_iobj0 operator++(int) = default;
+
+ void operator--(this xobj_iobj0& self)
+ { --self._v; }
+ xobj_iobj0 operator--(int) = default;
+};
+
+struct xobj_iobj1
+{
+ int _v;
+
+ void operator++(this xobj_iobj1& self)
+ { ++self._v; }
+ xobj_iobj1 operator++(int) & = default;
+
+ void operator--(this xobj_iobj1& self)
+ { --self._v; }
+ xobj_iobj1 operator--(int) & = default;
+};
+
+struct iobj_xobj0
+{
+ int _v;
+
+ void operator++()
+ { ++_v; }
+ iobj_xobj0 operator++(this iobj_xobj0&, int) = default;
+
+ void operator--()
+ { --_v; }
+ iobj_xobj0 operator--(this iobj_xobj0&, int) = default;
+};
+
+struct iobj_xobj1
+{
+ int _v;
+
+ void operator++() &
+ { ++_v; }
+ iobj_xobj1 operator++(this iobj_xobj1&, int) = default;
+
+ void operator--() &
+ { --_v; }
+ iobj_xobj1 operator--(this iobj_xobj1&, int) = default;
+};
+
+struct xobj_free
+{
+ int _v;
+
+ void operator++(this xobj_free& self)
+ { ++self._v; }
+
+ void operator--(this xobj_free& self)
+ { --self._v; }
+};
+
+xobj_free operator++(xobj_free&, int) = default;
+xobj_free operator--(xobj_free&, int) = default;
+
+
+struct free_xobj;
+void operator++(free_xobj& self);
+void operator--(free_xobj& self);
+
+struct free_xobj
+{
+ int _v;
+
+ free_xobj operator++(this free_xobj&, int) = default;
+ free_xobj operator--(this free_xobj&, int) = default;
+};
+
+void operator++(free_xobj& self)
+{ ++self._v; }
+
+void operator--(free_xobj& self)
+{ --self._v; }
+
+
+struct friend_xobj0
+{
+ int _v;
+
+ friend void operator++(friend_xobj0& self)
+ { ++self._v; }
+
+ friend void operator--(friend_xobj0& self)
+ { --self._v; }
+
+ friend_xobj0 operator++(this friend_xobj0&, int) = default;
+ friend_xobj0 operator--(this friend_xobj0&, int) = default;
+};
+
+
+struct friend_xobj1
+{
+ int _v;
+
+ friend void operator++(friend_xobj1& self);
+ friend void operator--(friend_xobj1& self);
+
+ friend_xobj1 operator++(this friend_xobj1&, int) = default;
+ friend_xobj1 operator--(this friend_xobj1&, int) = default;
+};
+
+void operator++(friend_xobj1& self)
+{ ++self._v; }
+
+void operator--(friend_xobj1& self)
+{ --self._v; }
+
+
+int main()
+{
+ xobj_iobj0 v0{42};
+ xobj_iobj1 v1{42};
+ iobj_xobj0 v2{42};
+ iobj_xobj1 v3{42};
+ xobj_free v4{42};
+ free_xobj v5{42};
+ friend_xobj0 v6{42};
+ friend_xobj1 v7{42};
+
+ // Sanity check.
+ if (v0._v != 42)
+ __builtin_abort ();
+ if (v1._v != 42)
+ __builtin_abort ();
+ if (v2._v != 42)
+ __builtin_abort ();
+ if (v3._v != 42)
+ __builtin_abort ();
+ if (v4._v != 42)
+ __builtin_abort ();
+ if (v5._v != 42)
+ __builtin_abort ();
+ if (v6._v != 42)
+ __builtin_abort ();
+ if (v7._v != 42)
+ __builtin_abort ();
+
+ if ((v0++)._v != 42)
+ __builtin_abort ();
+ if (v0._v != 43)
+ __builtin_abort ();
+ if ((v1++)._v != 42)
+ __builtin_abort ();
+ if (v1._v != 43)
+ __builtin_abort ();
+ if ((v2++)._v != 42)
+ __builtin_abort ();
+ if (v2._v != 43)
+ __builtin_abort ();
+ if ((v3++)._v != 42)
+ __builtin_abort ();
+ if (v3._v != 43)
+ __builtin_abort ();
+ if ((v4++)._v != 42)
+ __builtin_abort ();
+ if (v4._v != 43)
+ __builtin_abort ();
+ if ((v5++)._v != 42)
+ __builtin_abort ();
+ if (v5._v != 43)
+ __builtin_abort ();
+ if ((v6++)._v != 42)
+ __builtin_abort ();
+ if (v6._v != 43)
+ __builtin_abort ();
+ if ((v7++)._v != 42)
+ __builtin_abort ();
+ if (v7._v != 43)
+ __builtin_abort ();
+
+ if ((v0--)._v != 43)
+ __builtin_abort ();
+ if (v0._v != 42)
+ __builtin_abort ();
+ if ((v1--)._v != 43)
+ __builtin_abort ();
+ if (v1._v != 42)
+ __builtin_abort ();
+ if ((v2--)._v != 43)
+ __builtin_abort ();
+ if (v2._v != 42)
+ __builtin_abort ();
+ if ((v3--)._v != 43)
+ __builtin_abort ();
+ if (v3._v != 42)
+ __builtin_abort ();
+ if ((v4--)._v != 43)
+ __builtin_abort ();
+ if (v4._v != 42)
+ __builtin_abort ();
+ if ((v5--)._v != 43)
+ __builtin_abort ();
+ if (v5._v != 42)
+ __builtin_abort ();
+ if ((v6--)._v != 43)
+ __builtin_abort ();
+ if (v6._v != 42)
+ __builtin_abort ();
+ if ((v7--)._v != 43)
+ __builtin_abort ();
+ if (v7._v != 42)
+ __builtin_abort ();
+}
--
2.55.0