You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
some code refactored (`placement-new` rather than `memcpy`; `raw_address`
rather than `&_address[IPADDRESS_V4_BYTES_INDEX]`).
other parts of the C++ standard have been added and embedded into the code for discussion.
memcpy(&_address[IPADDRESS_V4_BYTES_INDEX], &address, 4); // This method guarantees a defined behavior. Any pointer conversions to write to ADDRESS storage (as a multibyte integer) are undefined behavior when the lifetime of the multibyte type has not previously started.
43
-
44
-
// C++ standard draft [basic.life#7](https://eel.is/c++draft/basic.life#7)
44
+
// memcpy(raw_address(), &address, 4); // This method guarantees a defined behavior.
// 13 An operation that begins the lifetime of an array of unsigned char or std::byte implicitly creates objects within the
60
+
// region of storage occupied by the array.
61
+
// [Note 5: The array object provides storage for these objects. — end note]
62
+
63
+
// Current draft: https://eel.is/c++draft/intro.object#14
64
+
// 14 Except during constant evaluation, an operation that begins the lifetime of an array of unsigned char or std::byte implicitly
65
+
// creates objects within the region of storage occupied by the array.
66
+
// [Note 5: The array object provides storage for these objects. — end note]
67
+
68
+
// *reinterpret_cast<uint32_t*>(_address[IPADDRESS_V4_BYTES_INDEX]) = address; // This valid initialization in the `_address` storage since C++20.
69
+
// now the pointer `_address[IPADDRESS_V4_BYTES_INDEX]` points to a multibyte int.
70
+
71
+
new (&_address[IPADDRESS_V4_BYTES_INDEX]) uint32_t (address); // But the new-expression is better for understanding and looks nicer (for trivial types, the
72
+
// new expression only begins its lifetime and does not perform any other actions).
0 commit comments