From cppreference.com
template<classT,classU>structis_layout_compatible;(since C++20)If T and U are
types, provides the member constant value equal to true. Otherwise value is false.
Every type is layout-compatible with its any cv-qualified versions, even if it is not an object type.
If T or U is not a complete type, (possibly cv-qualified) void, or an array of unknown bound, the behavior is undefined.
If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
If the program adds specializations for std::is_layout_compatible or std::is_layout_compatible_v, the behavior is undefined.
Helper variable template
template<classT,classU>constexprboolis_layout_compatible_v=is_layout_compatible<T,U>::value;(since C++20)Inherited from
Member constants
value
[static]
true if T and U are layout-compatible, false otherwise
(public static member constant)Member functions
operator bool
converts the object to bool, returns value
(public member function)operator()
(C++14)
returns value
(public member function)Member types
Type Definition value_typebooltypestd::integral_constant<bool,value>Notes
A signed integer type and its unsigned counterpart are not layout-compatible. char is layout-compatible with neither signedchar nor unsignedchar.
are not layout-compatible if they are not the same type after ignoring top-level cv-qualification.
An enumeration type and its underlying type are not layout-compatible.
Array types of layout-compatible but different element types (ignoring cv-qualification) are not layout-compatible, even if they are of equal length.
macroValueStdFeature
__cpp_lib_is_layout_compatible
(C++20)std::is_layout_compatibleExample
Run this code
#include<type_traits>static_assert(""&&std::is_layout_compatible_v<constvoid,volatilevoid>&&!std::is_layout_compatible_v<long,unsignedlong>&&std::is_layout_compatible_v<char*,char*const>&&!std::is_layout_compatible_v<char*,constchar*>);structFoo{intx;chary;};structFooNua{intx;[[no_unique_address]]chary;};classBar{constintu{42};volatilecharv{'*'};};static_assert(""&&std::is_layout_compatible_v<Foo,Bar>&&!std::is_layout_compatible_v<Foo[2],Bar[2]>&&!std::is_layout_compatible_v<Foo,FooNua>//Note [1]);//[1] MSVC erroneously fails this assertenumE0:int{};enumclassE1:int{};static_assert(""&&!std::is_layout_compatible_v<int,E0>&&std::is_layout_compatible_v<E0,E1>&&!std::is_layout_compatible_v<int,E1>);intmain(){}See also