[clang] [clang] Implement a bitwise_copyable builtin type trait. (PR #86512)
Ilya Biryukov via cfe-commits
cfe-commits at lists.llvm.org
Tue May 28 09:21:27 PDT 2024
================
@@ -4016,6 +4016,34 @@ Note that the `size` argument must be a compile time constant.
Note that this intrinsic cannot yet be called in a ``constexpr`` context.
+``__is_bitwise_cloneable``
+--------------------------
+
+A type trait is used to check whether a type can be safely copied by memcpy.
+
+**Syntax**:
+
+.. code-block:: c++
+
+ bool __is_bitwise_cloneable(Type)
+
+**Description**:
+
+This trait is similar to `std::is_trivially_copyable`, but additionally allows
+to have user-defined constructors, virtual functions and virtual bases. It is up
+to the user code to guarantee that a bitwise copy results in non-broken object
+and that the lifetime of an object is properly started.
+
+Objects of bitwise cloneable types can be bitwise copied by memcpy/memmove. The
+Clang compiler warrants that this behavior is well defined, and won't be
+broken by compiler optimizations.
+
+After the copy, the lifetime of the new object isn't started yet (unless the
+type is trivially copyable). Users must ensure its lifetime is started to avoid
+undefined behavior.
----------------
ilya-biryukov wrote:
> Users must ensure its lifetime is started
What should the intended action taken by the user be? Is this advice actionable?
I guess that if the type is implicit-lifetime-type, users could use `start_lifetime_as` (although it's not implemented in Clang).
AFAIK, there is no way to start a lifetime of other objects (e.g. containing virtual members).
I suggest to either mention the `start_lifetime_as` or simply say that it's still undefined behavior to use this object, i.e.
```
Note that after the copy the lifetime of the new object isn't started and using it
is undefined behavior from the C++ Standard's perspective. In C++23 `std::start_lifetime_as`
can start the lifetime of implicit-lifetime types. There is no standard way to start a lifetime of
other objects, e.g. that have a vtable.
```
https://github.com/llvm/llvm-project/pull/86512
More information about the cfe-commits
mailing list