| 1 |
// P0847R7 |
| 2 |
// { dg-do run { target c++23 } } |
| 3 |
|
| 4 |
// correct constructor selection when initializing a by value xobj parameter |
| 5 |
|
| 6 |
// see explicit-obj-by-value1.C for details on this test |
| 7 |
|
| 8 |
using uintptr_t = __UINTPTR_TYPE__; |
| 9 |
inline constexpr uintptr_t magic = 42; |
| 10 |
inline constexpr uintptr_t copy_magic = 5; |
| 11 |
inline constexpr uintptr_t move_magic = 10; |
| 12 |
|
| 13 |
struct S { |
| 14 |
uintptr_t _v; |
| 15 |
explicit S(uintptr_t v) : _v(v) {} |
| 16 |
S(S const& other) : _v(other._v + copy_magic) {} |
| 17 |
S(S&& other) : _v(other._v + move_magic) {} |
| 18 |
uintptr_t f(this S self) { |
| 19 |
return self._v; |
| 20 |
} |
| 21 |
}; |
| 22 |
|
| 23 |
int main() |
| 24 |
{ |
| 25 |
S s0{magic}; |
| 26 |
S s1{magic}; |
| 27 |
// prevent (absurdly improbable (^2)) bogus results |
| 28 |
// it's virtually impossible for both to have a bogus result, |
| 29 |
// but we can guarantee correct results from both easily, so why not? |
| 30 |
S& s_copy_from = magic + copy_magic != (uintptr_t)(&s0) ? s0 : s1; |
| 31 |
S& s_move_from = magic + move_magic != (uintptr_t)(&s0) ? s0 : s1; |
| 32 |
uintptr_t const copy_ret = static_cast<S const&>(s_copy_from).f(); |
| 33 |
uintptr_t const move_ret = static_cast<S&&>(s_move_from).f(); |
| 34 |
// we test specifically for reinterpretation in other |
| 35 |
// by value tests, it's unnecessary to do it again here |
| 36 |
if (copy_ret != magic + copy_magic) |
| 37 |
__builtin_abort (); |
| 38 |
if (move_ret != magic + move_magic) |
| 39 |
__builtin_abort (); |
| 40 |
} |
| 41 |
|