forked from ruyisdk/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
c++: P0847R7 (deducing this) - CWG2586 [PR102609]
This adds support for defaulted comparison operators and copy/move assignment operators, as well as allowing user defined xobj copy/move assignment operators. It turns out defaulted comparison operators already worked though, so this just adds a test for them. Defaulted comparison operators were not so nice and required a bit of a hack. Should work fine though! The diagnostics leave something to be desired, and there are some things that could be improved with more extensive design changes. There are a few notes left indicating where I think we could make improvements. Aside from some small bugs, with this commit xobj member functions should be feature complete. PR c++/102609 gcc/cp/ChangeLog: PR c++/102609 C++23 P0847R7 (deducing this) - CWG2586. * decl.cc (copy_fn_p): Accept xobj copy assignment functions. (move_signature_fn_p): Accept xobj move assignment functions. * method.cc (do_build_copy_assign): Handle defaulted xobj member functions. (defaulted_late_check): Comment. (defaultable_fn_check): Comment. gcc/testsuite/ChangeLog: PR c++/102609 C++23 P0847R7 (deducing this) - CWG2586. * g++.dg/cpp23/explicit-obj-basic6.C: New test. * g++.dg/cpp23/explicit-obj-default1.C: New test. * g++.dg/cpp23/explicit-obj-default2.C: New test. Signed-off-by: Waffl3x <waffl3x@protonmail.com>
- Loading branch information
1 parent
42b8b69
commit 52976a5
Showing
5 changed files
with
248 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// P0847R7 | ||
// { dg-do compile { target c++23 } } | ||
|
||
// user defined copy/move assignment operators | ||
|
||
inline constexpr int add_when_copy = 5; | ||
inline constexpr int add_when_move = 10; | ||
inline constexpr int poison = -1; | ||
|
||
struct S { | ||
int _v; | ||
S& operator=(this S& self, S const& rhs) { | ||
self._v = rhs._v + add_when_copy; | ||
return self; | ||
}; | ||
S& operator=(this S& self, S&& rhs) { | ||
self._v = rhs._v + add_when_move; | ||
rhs._v = poison; | ||
return self; | ||
} | ||
}; | ||
|
||
inline constexpr int init_val = 5; | ||
|
||
int main() | ||
{ | ||
S s0{0}; | ||
S s1{init_val}; | ||
|
||
// Sanity check. | ||
if (s0._v != 0 | ||
|| s1._v != init_val) | ||
__builtin_abort (); | ||
|
||
s0 = s1; | ||
if (s0._v != init_val + add_when_copy) | ||
__builtin_abort (); | ||
if (s1._v != init_val) | ||
__builtin_abort (); | ||
|
||
s0 = S{init_val}; | ||
if (s0._v != init_val + add_when_move) | ||
__builtin_abort (); | ||
|
||
S s2{init_val}; | ||
s0 = static_cast<S&&>(s2); | ||
if (s0._v != init_val + add_when_move) | ||
__builtin_abort (); | ||
if (s2._v != poison) | ||
__builtin_abort (); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// P0847R7 | ||
// { dg-do run { target c++23 } } | ||
|
||
// defaulted comparison operators | ||
|
||
#include <compare> | ||
|
||
struct S { | ||
int _v; | ||
bool operator==(this S const&, S const&) = default; | ||
auto operator<=>(this S const&, S const&) = default; | ||
}; | ||
|
||
int main() | ||
{ | ||
S const a_10{10}; | ||
S const b_10{10}; | ||
S const c_20{20}; | ||
S const d_5{5}; | ||
|
||
if (a_10 != b_10) | ||
__builtin_abort (); | ||
if (c_20 == a_10) | ||
__builtin_abort (); | ||
if (!(a_10 == b_10)) | ||
__builtin_abort (); | ||
if (!(c_20 != a_10)) | ||
__builtin_abort (); | ||
|
||
if (a_10 < b_10) | ||
__builtin_abort (); | ||
if (a_10 > b_10) | ||
__builtin_abort (); | ||
if (!(a_10 <= b_10)) | ||
__builtin_abort (); | ||
if (!(a_10 >= b_10)) | ||
__builtin_abort (); | ||
|
||
if (!(a_10 < c_20)) | ||
__builtin_abort (); | ||
if (a_10 > c_20) | ||
__builtin_abort (); | ||
if (!(a_10 <= c_20)) | ||
__builtin_abort (); | ||
if (a_10 >= c_20) | ||
__builtin_abort (); | ||
|
||
if (a_10 < d_5) | ||
__builtin_abort (); | ||
if (!(a_10 > d_5)) | ||
__builtin_abort (); | ||
if (a_10 <= d_5) | ||
__builtin_abort (); | ||
if (!(a_10 >= d_5)) | ||
__builtin_abort (); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// P0847R7 | ||
// { dg-do run { target c++23 } } | ||
|
||
// defaulted copy/move assignment operators | ||
|
||
inline constexpr int add_when_copy = 5; | ||
inline constexpr int add_when_move = 10; | ||
inline constexpr int poison = -1; | ||
|
||
struct A { | ||
int _v; | ||
A(int v) : _v(v) {} | ||
A& operator=(A const& rhs) { | ||
if (&rhs == this) | ||
return *this; | ||
_v = rhs._v + add_when_copy; | ||
return *this; | ||
} | ||
A& operator=(A&& rhs) { | ||
if (&rhs == this) | ||
return *this; | ||
_v = rhs._v + add_when_move; | ||
rhs._v = poison; | ||
return *this; | ||
} | ||
}; | ||
|
||
struct S { | ||
A _a; | ||
S& operator=(this S&, S const&) = default; | ||
S& operator=(this S&, S&&) = default; | ||
|
||
int v() const { return _a._v; } | ||
}; | ||
|
||
inline constexpr int init_val = 5; | ||
|
||
int main() | ||
{ | ||
S s0{0}; | ||
S s1{init_val}; | ||
|
||
// Sanity check. | ||
if (s0.v () != 0 | ||
|| s1.v () != init_val) | ||
__builtin_abort (); | ||
|
||
s0 = s1; | ||
if (s0.v () != init_val + add_when_copy) | ||
__builtin_abort (); | ||
if (s1.v () != init_val) | ||
__builtin_abort (); | ||
|
||
s0 = S{init_val}; | ||
if (s0.v () != init_val + add_when_move) | ||
__builtin_abort (); | ||
|
||
S s2{init_val}; | ||
s0 = static_cast<S&&>(s2); | ||
if (s0.v () != init_val + add_when_move) | ||
__builtin_abort (); | ||
if (s2.v () != poison) | ||
__builtin_abort (); | ||
} | ||
|