std::is_standard_layout
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   在标头  <type_traits> 定义
  | 
||
|   template< class T > struct is_standard_layout;  | 
(C++11 起) | |
如果 T 是标准布局类型,那么提供等于 true 的成员常量 value。对任何其他类型,value 是 false。
如果 std::remove_all_extents_t<T> 是不完整类型且不是(可有 cv 限定的)void,那么行为未定义。
添加 is_standard_layout 或 is_standard_layout_v (C++17 起) 的特化的程序行为未定义。
模板形参
| T | - | 要检查的类型 | 
辅助变量模板
|   template< class T > inline constexpr bool is_standard_layout_v = is_standard_layout<T>::value;  | 
(C++17 起) | |
继承自 std::integral_constant
成员常量
|    value [静态]  | 
   若 T 是标准布局类型则为 true ,否则为 false  (公开静态成员常量)  | 
成员函数
|    operator bool  | 
   转换对象为 bool ,返回 value  (公开成员函数)  | 
|    operator() (C++14)  | 
   返回 value  (公开成员函数)  | 
成员类型
| 类型 | 定义 | 
  value_type
 | 
  bool
 | 
  type
 | 
std::integral_constant<bool, value> | 
注解
指向标准布局类的指针能转换(以 reinterpret_cast)成指向其首个非静态数据成员的指针,反之亦然。
如果标准布局联合体保有多个标准布局结构体,那么可以查看它们的公共前导部分。
宏 offsetof 只保证能用于标准布局类。
示例
运行此代码
#include <iostream> #include <type_traits> struct A { int m; }; struct B { int m1; private: int m2; }; struct C { virtual void foo(); }; int main() { std::cout << std::boolalpha; std::cout << std::is_standard_layout<A>::value << '\n'; std::cout << std::is_standard_layout<B>::value << '\n'; std::cout << std::is_standard_layout<C>::value << '\n'; }
输出:
true false false
参阅
|    (C++11)  | 
  检查类型是否可平凡复制  (类模板)  | 
|    (C++11)(C++20 中弃用)  | 
  检查类型是否为简旧数据(POD)类型  (类模板)  | 
|    从标准布局类型的起始到其指定成员的字节偏移量  (宏函数)  |