Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::is_layout_compatible

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (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
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (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)

      IfT andU arelayout-compatible types, provides the member constantvalue equal totrue. Otherwisevalue isfalse.

      Every type is layout-compatible with its any cv-qualified versions, even if it is not an object type.

      IfT orU 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 forstd::is_layout_compatible orstd::is_layout_compatible_v, the behavior is undefined.

      Contents

      [edit]Helper variable template

      template<class T,class U>
      constexprbool is_layout_compatible_v= is_layout_compatible<T, U>::value;
      (since C++20)
      [edit]

      Inherited fromstd::integral_constant

      Member constants

      value
      [static]
      true ifT andU are layout-compatible,false otherwise
      (public static member constant)

      Member functions

      operator bool
      converts the object tobool, returnsvalue
      (public member function)
      operator()
      (C++14)
      returnsvalue
      (public member function)

      Member types

      Type Definition
      value_typebool
      typestd::integral_constant<bool, value>

      [edit]Notes

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

      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 macroValueStdFeature
      __cpp_lib_is_layout_compatible201907L(C++20)std::is_layout_compatible

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <type_traits> struct Foo{int x;char y;}; struct FooNua{int x;[[no_unique_address]]char y;}; class Bar{constint u=42;volatilechar v='*';}; enum E0:int{};enumclass E1:int{}; static_assert(    std::is_layout_compatible_v<constvoid,volatilevoid>==true  and    std::is_layout_compatible_v<Foo, Bar>==true  and    std::is_layout_compatible_v<Foo[2], Bar[2]>==false and    std::is_layout_compatible_v<int, E0>==false and    std::is_layout_compatible_v<E0, E1>==true  and    std::is_layout_compatible_v<long,unsignedlong>==false and    std::is_layout_compatible_v<char*,constchar*>==false and    std::is_layout_compatible_v<char*,char*const>==true  and    std::is_layout_compatible_v<Foo, FooNua>==false// Note [1]); // [1] MSVC erroneously fails this assert int main(){}

      [edit]See also

      checks if a type is astandard-layout type
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/is_layout_compatible&oldid=176470"

      [8]ページ先頭

      ©2009-2025 Movatter.jp