std:: is_layout_compatible
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T,
class
U
>
struct is_layout_compatible ; |
(C++20 起) | |
如果
T
和
U
是
布局兼容
类型,则提供等于
true
的成员常量
value
。否则
value
为
false
。
每个类型都与其任何 cv 限定版本布局兼容,即使该类型不是对象类型。
如果
T
或
U
不是完整类型、(可能带有 cv 限定符的)
void
,或未知边界数组,则行为未定义。
如果上述模板的实例化直接或间接依赖于不完整类型,且该实例化在该类型被假设完成时可能产生不同结果,则行为未定义。
如果程序为
std::is_layout_compatible
或
std::is_layout_compatible_v
添加特化,则行为未定义。
目录 |
辅助变量模板
|
template
<
class
T,
class
U
>
constexpr bool is_layout_compatible_v = is_layout_compatible < T, U > :: value ; |
(C++20 起) | |
继承自 std:: integral_constant
成员常量
|
value
[static]
|
若
T
与
U
布局兼容则为
true
,否则为
false
(公开静态成员常量) |
成员函数
|
operator bool
|
转换对象为
bool
,返回
value
(公开成员函数) |
|
operator()
(C++14)
|
返回
value
(公开成员函数) |
成员类型
| 类型 | 定义 |
value_type
|
bool |
type
|
std:: integral_constant < bool , value > |
注释
有符号整数类型与其对应的无符号类型不具有布局兼容性。 char 与 signed char 及 unsigned char 均不构成布局兼容关系。
相似类型 如果在忽略顶层 cv 限定符后不是同一类型,则不具备布局兼容性。
枚举类型与其底层类型不满足布局兼容性要求。
具有布局兼容但元素类型不同(忽略 cv 限定符)的数组类型不是布局兼容的,即使它们的长度相等。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_is_layout_compatible
|
201907L
|
(C++20) |
std::is_layout_compatible
|
示例
#include <iomanip> #include <iostream> #include <type_traits> struct Foo { int x; char y; }; struct FooNua { int x; [[no_unique_address]] char y; }; class Bar { const int u = 42; volatile char v = '*'; }; enum E0 : int {}; enum class E1 : int {}; static_assert ( std::is_layout_compatible_v<const void, volatile void> == 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, unsigned long> == false and std::is_layout_compatible_v<char*, const char*> == false and std::is_layout_compatible_v<char*, char* const> == true and std::is_layout_compatible_v<Foo, FooNua> == false // 注意 [1] ); // [1] MSVC 错误地未能通过此断言 int main() {}
参见
|
(C++11)
|
检查类型是否为
标准布局
类型
(类模板) |