Namespaces
Variants

std:: is_standard_layout

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
is_standard_layout
(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
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
定义于头文件 <type_traits>
template < class T >
struct is_standard_layout ;
(C++11 起)

std::is_standard_layout 是一个 UnaryTypeTrait

如果 T 标准布局类型 ,则提供等于 true 的成员常量 value 。对于任何其他类型, value false

如果 std:: remove_all_extents_t < T > 是不完整类型且非(可能带有 cv 限定符的) void ,则行为未定义。

如果程序为 std::is_standard_layout std::is_standard_layout_v 添加特化,则行为是未定义的。

目录

模板参数

T - 待检查的类型

辅助变量模板

template < class T >
constexpr bool is_standard_layout_v = is_standard_layout < T > :: value ;
(C++17 起)

继承自 std:: integral_constant

成员常量

value
[static]
T 为标准布局类型则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
转换对象为 bool 类型,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
value_type bool
type std:: integral_constant < bool , value >

注释

指向标准布局类的指针可以(通过 reinterpret_cast )转换为其首个非静态数据成员的指针(参见 空基类优化 ),反之亦然。

如果标准布局联合体包含两个或更多标准布局结构体,允许检查它们的共同起始部分。

offsetof 仅保证可用于标准布局类。

示例

#include <type_traits>
struct A { int m; };
static_assert(std::is_standard_layout_v<A> == true);
class B: public A { int m; };
static_assert(std::is_standard_layout_v<B> == false);
struct C { virtual void foo(); };
static_assert(std::is_standard_layout_v<C> == false);
int main() {}

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2015 C++11 T 可能是未知边界的
不完整类类型的数组
此情况下行为
未定义

参见

检查类型是否为可平凡复制的
(类模板)
(C++11) (C++20 中弃用)
检查类型是否为简旧数据 (POD) 类型
(类模板)
标准布局 类型起始到指定成员的字节偏移量
(函数宏)