std::has_unique_object_representations
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct has_unique_object_representations; |
(C++17 起) | |
std::has_unique_object_representations
是一個 一元型別特性 (UnaryTypeTrait)。
如果 T
是可平凡複製的 (trivially copyable),並且任何兩個型別為 T
的物件,如果它們的值相同,則它們的物件表示 (object representation)也相同,則提供成員常量 value
等於 true。對於任何其他型別,value
為 false。
對於此特性,如果兩個陣列的元素值相同,則它們具有相同的值;如果兩個非聯合類的直接子物件具有相同的值,則它們具有相同的值;如果兩個聯合具有相同的活動成員,且該成員的值相同,則它們具有相同的值。
哪些標量型別滿足此特性是實現定義的,但是無符號(直到 C++20)不使用填充位的整數型別保證具有唯一的物件表示。
如果 std::remove_all_extents_t<T> 是一個不完整型別,而不是(可能帶有 cv 限定符的)void,則行為未定義。
如果程式為 std::has_unique_object_representations
或 std::has_unique_object_representations_v
新增特化,則行為未定義。
目錄 |
[編輯] 模板引數
T | - | 要檢查的型別 |
[編輯] 輔助變數模板
template< class T > constexpr bool has_unique_object_representations_v = |
(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> |
[編輯] 註解
引入此特性是為了判斷一個型別是否可以透過將其物件表示作為位元組陣列進行雜湊來正確地進行雜湊。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_has_unique_object_representations |
201606L |
(C++17) | std::has_unique_object_representations
|
[編輯] 示例
執行此程式碼
#include <cstdint> #include <type_traits> struct unpadded { std::uint32_t a, b; }; struct likely_padded { std::uint8_t c; std::uint16_t st; std::uint32_t i; }; int main() { // Every value of a char corresponds to exactly one object representation. static_assert(std::has_unique_object_representations_v<char>); // For IEC 559 floats, assertion passes because the value NaN has // multiple object representations. static_assert(!std::has_unique_object_representations_v<float>); // Should succeed in any sane implementation because unpadded // is typically not padded, and std::uint32_t cannot contain padding bits. static_assert(std::has_unique_object_representations_v<unpadded>); // Fails in most implementations because padding bits are inserted // between the data members c and st for the purpose of aligning st to 16 bits. static_assert(!std::has_unique_object_representations_v<likely_padded>); // Notable architectural divergence: static_assert(std::has_unique_object_representations_v<bool>); // x86 // static_assert(!std::has_unique_object_representations_v<bool>); // ARM }
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 4113 | C++17 | T 可以是未知邊界的陣列即使其元素型別不完整 |
要求元素 型別完整 |
[編輯] 參閱
(C++11) |
檢查型別是否為標準佈局型別 (類模板) |
(C++11) |
雜湊函式物件 (類模板) |