|
| 1 | +// translation-fail: refcount, unsafe |
| 2 | +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 |
| 3 | +#include <cassert> |
| 4 | +#include <cstring> |
| 5 | + |
| 6 | +// N4950 [basic.types.general]/2: the underlying bytes of an object of |
| 7 | +// trivially copyable type can be copied into an array of unsigned char; if |
| 8 | +// they are copied back, the object holds its original value. |
| 9 | +namespace p2_pointer_to_member_function_bytes_round_trip { |
| 10 | +struct HasTwoMemberFunctions { |
| 11 | + int first_calls = 0; |
| 12 | + int second_calls = 0; |
| 13 | + void first() { ++first_calls; } |
| 14 | + void second() { ++second_calls; } |
| 15 | +}; |
| 16 | + |
| 17 | +void test() { |
| 18 | + void (HasTwoMemberFunctions::*pmf)() = &HasTwoMemberFunctions::second; |
| 19 | + unsigned char bytes[sizeof(pmf)]; |
| 20 | + std::memcpy(bytes, &pmf, sizeof(pmf)); |
| 21 | + pmf = &HasTwoMemberFunctions::first; |
| 22 | + std::memcpy(&pmf, bytes, sizeof(pmf)); |
| 23 | + assert(pmf == &HasTwoMemberFunctions::second); |
| 24 | + HasTwoMemberFunctions obj; |
| 25 | + (obj.*pmf)(); |
| 26 | + assert(obj.first_calls == 0); |
| 27 | + assert(obj.second_calls == 1); |
| 28 | + |
| 29 | + void (HasTwoMemberFunctions::*null_pmf)() = nullptr; |
| 30 | + std::memcpy(bytes, &null_pmf, sizeof(null_pmf)); |
| 31 | + null_pmf = &HasTwoMemberFunctions::first; |
| 32 | + std::memcpy(&null_pmf, bytes, sizeof(null_pmf)); |
| 33 | + assert(null_pmf == nullptr); |
| 34 | +} |
| 35 | +} // namespace p2_pointer_to_member_function_bytes_round_trip |
| 36 | + |
| 37 | +// N4950 [basic.types.general]/2: the underlying bytes of an object of |
| 38 | +// trivially copyable type can be copied into an array of unsigned char; if |
| 39 | +// they are copied back, the object holds its original value. |
| 40 | +namespace p2_pointer_to_virtual_member_function_bytes_round_trip { |
| 41 | +static int base_calls; |
| 42 | +static int override_calls; |
| 43 | + |
| 44 | +struct DeclaresVirtual { |
| 45 | + virtual void run() { ++base_calls; } |
| 46 | + void other() {} |
| 47 | +}; |
| 48 | + |
| 49 | +struct OverridesVirtual : DeclaresVirtual { |
| 50 | + void run() override { ++override_calls; } |
| 51 | +}; |
| 52 | + |
| 53 | +void test() { |
| 54 | + void (DeclaresVirtual::*pmf)() = &DeclaresVirtual::run; |
| 55 | + unsigned char bytes[sizeof(pmf)]; |
| 56 | + std::memcpy(bytes, &pmf, sizeof(pmf)); |
| 57 | + pmf = &DeclaresVirtual::other; |
| 58 | + std::memcpy(&pmf, bytes, sizeof(pmf)); |
| 59 | + OverridesVirtual overrider; |
| 60 | + DeclaresVirtual &ref = overrider; |
| 61 | + (ref.*pmf)(); |
| 62 | + assert(base_calls == 0); |
| 63 | + assert(override_calls == 1); |
| 64 | +} |
| 65 | +} // namespace p2_pointer_to_virtual_member_function_bytes_round_trip |
| 66 | + |
| 67 | +// N4950 [basic.types.general]/3: if the underlying bytes of obj1 of trivially |
| 68 | +// copyable type are copied into obj2, obj2 holds the same value as obj1. |
| 69 | +namespace p3_class_holding_pointers_to_members_bytes_copied { |
| 70 | +struct HasValueAndIncrement { |
| 71 | + int padding; |
| 72 | + int value; |
| 73 | + void increment() { ++value; } |
| 74 | +}; |
| 75 | + |
| 76 | +struct HoldsPointersToMembers { |
| 77 | + int HasValueAndIncrement::*data; |
| 78 | + void (HasValueAndIncrement::*function)(); |
| 79 | +}; |
| 80 | + |
| 81 | +void test() { |
| 82 | + HoldsPointersToMembers source{&HasValueAndIncrement::value, |
| 83 | + &HasValueAndIncrement::increment}; |
| 84 | + HoldsPointersToMembers destination{nullptr, nullptr}; |
| 85 | + std::memcpy(&destination, &source, sizeof(source)); |
| 86 | + assert(destination.data == &HasValueAndIncrement::value); |
| 87 | + assert(destination.function == &HasValueAndIncrement::increment); |
| 88 | + HasValueAndIncrement obj{1, 2}; |
| 89 | + (obj.*destination.function)(); |
| 90 | + assert(obj.*destination.data == 3); |
| 91 | + assert(obj.padding == 1); |
| 92 | +} |
| 93 | +} // namespace p3_class_holding_pointers_to_members_bytes_copied |
| 94 | + |
| 95 | +// N4950 [basic.types.general]/3: if the underlying bytes of obj1 of trivially |
| 96 | +// copyable type are copied into obj2, obj2 holds the same value as obj1. |
| 97 | +namespace p3_pointer_to_member_function_bytes_copied_to_other_object { |
| 98 | +struct HasTwoMemberFunctions { |
| 99 | + int first_calls = 0; |
| 100 | + int second_calls = 0; |
| 101 | + void first() { ++first_calls; } |
| 102 | + void second() { ++second_calls; } |
| 103 | +}; |
| 104 | + |
| 105 | +void test() { |
| 106 | + void (HasTwoMemberFunctions::*source)() = &HasTwoMemberFunctions::second; |
| 107 | + void (HasTwoMemberFunctions::*destination)() = &HasTwoMemberFunctions::first; |
| 108 | + std::memcpy(&destination, &source, sizeof(source)); |
| 109 | + assert(destination == source); |
| 110 | + HasTwoMemberFunctions obj; |
| 111 | + (obj.*destination)(); |
| 112 | + assert(obj.first_calls == 0); |
| 113 | + assert(obj.second_calls == 1); |
| 114 | + |
| 115 | + void (HasTwoMemberFunctions::*null_source)() = nullptr; |
| 116 | + std::memcpy(&destination, &null_source, sizeof(null_source)); |
| 117 | + assert(destination == nullptr); |
| 118 | +} |
| 119 | +} // namespace p3_pointer_to_member_function_bytes_copied_to_other_object |
| 120 | + |
| 121 | +int main() { |
| 122 | + p2_pointer_to_member_function_bytes_round_trip::test(); |
| 123 | + p2_pointer_to_virtual_member_function_bytes_round_trip::test(); |
| 124 | + p3_class_holding_pointers_to_members_bytes_copied::test(); |
| 125 | + p3_pointer_to_member_function_bytes_copied_to_other_object::test(); |
| 126 | + return 0; |
| 127 | +} |
0 commit comments