Namespaces
Variants

std::meta::is_layout_compatible_type

From cppreference.com
< cpp | meta
 
 
 
Reflection library
 
Reflection types and queries
Type properties
Type property queries
 
Defined in header <meta>
consteval bool  is_layout_compatible_type( std::meta::info r1, std::meta::info r2 );
(since C++26)

Returns true if types T and U, represented by r1 and r2, are layout-compatible types. Otherwise, returns false.

Equivalent to return std::is_layout_compatible_v<T, U>;.

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 program is ill-formed.

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 program is ill-formed.

Commutativity is satisfied, i.e., for any two reflections:

is_layout_compatible_type(r1, r2) == true if and only if

is_layout_compatible_type(r2, r1) == true.

Parameters

r1, r2 - reflection values to check

Return value

true if r1 and r2 represent layout-compatible types, false otherwise.

Exceptions

Throws std::meta::exception if either r1 or r2 does not represent a type or type alias.

Notes

A signed integer type and its unsigned counterpart are not layout-compatible. char is layout-compatible with neither signed char nor unsigned char.

Similar types 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.

Example

#include <meta>

static_assert
(""
    && is_layout_compatible_type(^^const void, ^^volatile void)
    && !is_layout_compatible_type(^^long, ^^unsigned long)
    && is_layout_compatible_type(^^char*, ^^char* const)
    && !is_layout_compatible_type(^^char*, ^^const char*)
);

struct Foo
{
    int x;
    char y;
};

struct FooNua
{
    int x;
    [[no_unique_address]] char y;
};

class Bar
{
    const int u{42};
    volatile char v{'*'};
};

static_assert
(""
    && is_layout_compatible_type(^^Foo, ^^Bar)
    && !is_layout_compatible_type(^^Foo[2], ^^Bar[2])
    && !is_layout_compatible_type(^^Foo, ^^FooNua)
);

enum E0 : int {};
enum class E1 : int {};
static_assert
(""
    && !is_layout_compatible_type(^^int, ^^E0)
    && is_layout_compatible_type(^^E0, ^^E1)
    && !is_layout_compatible_type(^^int, ^^E0)
    && is_layout_compatible_type(^^E0, ^^E1)
);

int main() {}

See also

checks if reflected type is a standard-layout type
(function) [edit]
checks if two types are layout-compatible
(class template) [edit]