std::is_standard_layout
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_standard_layout; |
(C++11 起) | |
std::is_standard_layout
是一個 UnaryTypeTrait。
如果 T
是一個 標準佈局型別,則提供成員常量 value
,其值為 true。對於任何其他型別,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 [靜態] |
true 如果 T 是標準佈局型別,否則為 false(public static 成員常量) |
成員函式
operator bool |
將物件轉換為 bool,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
bool |
型別
|
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++11)(C++20 中已棄用) |
檢查型別是否為純舊資料 (POD) 型別 (類模板) |
標準佈局型別到指定成員的位元組偏移量 (函式宏) |