std::is_layout_compatible

From cppreference.com
< cpp‎ | types
 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)
(C++17)

(C++11)(until C++20)(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 
Defined in header <type_traits>
template< class T, class U >
struct is_layout_compatible;
(since C++20)

If T and U are layout-compatible 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.

T and U shall each be a complete type, (possibly cv-qualified) void, or an array of unknown bound. Otherwise, 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.

The behavior of a program that adds specializations for is_layout_compatible or is_layout_compatible_v is undefined.

Helper variable template

template< class T, class U >
inline constexpr bool is_layout_compatible_v = is_layout_compatible<T, U>::value;
(since C++20)

Inherited from std::integral_constant

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_type bool
type std::integral_constant<bool, value>

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.

Feature-test macro: __cpp_lib_is_layout_compatible

Example

#include <type_traits>
#include <iomanip>
#include <iostream>
 
struct Foo {
    int x;
    char y;
};
 
class Bar {
    const int u = 42;
    volatile char v = '*';
};
 
enum E0 : int {};
enum class E1 : int {};
 
#define SHOW(...) std::cout << std::setw(54) << #__VA_ARGS__ << " = " << __VA_ARGS__ << '\n'
 
int main()
{
    std::cout << std::boolalpha << std::left;
    SHOW(std::is_layout_compatible_v<const void, volatile void>);
    SHOW(std::is_layout_compatible_v<Foo, Bar>);
    SHOW(std::is_layout_compatible_v<Foo[2], Bar[2]>);
    SHOW(std::is_layout_compatible_v<int, E0>);
    SHOW(std::is_layout_compatible_v<E0, E1>);
    SHOW(std::is_layout_compatible_v<long, unsigned long>);
    SHOW(std::is_layout_compatible_v<char*, const char*>);
    SHOW(std::is_layout_compatible_v<char*, char* const>);
}

Output:

std::is_layout_compatible_v<const void, volatile void> = true
std::is_layout_compatible_v<Foo, Bar>                  = true
std::is_layout_compatible_v<Foo[2], Bar[2]>            = false
std::is_layout_compatible_v<int, E0>                   = false
std::is_layout_compatible_v<E0, E1>                    = true
std::is_layout_compatible_v<long, unsigned long>       = false
std::is_layout_compatible_v<char*, const char*>        = false
std::is_layout_compatible_v<char*, char* const>        = true

See also

checks if a type is a standard-layout type
(class template)