名稱空間
變體
操作

std::has_unique_object_representations

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
has_unique_object_representations
(C++17)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <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。對於任何其他型別,valuefalse

對於此特性,如果兩個陣列的元素值相同,則它們具有相同的值;如果兩個非聯合類的直接子物件具有相同的值,則它們具有相同的值;如果兩個聯合具有相同的活動成員,且該成員的值相同,則它們具有相同的值。

哪些標量型別滿足此特性是實現定義的,但是無符號(直到 C++20)不使用填充位的整數型別保證具有唯一的物件表示。

如果 std::remove_all_extents_t<T> 是一個不完整型別,而不是(可能帶有 cv 限定符的)void,則行為未定義。

如果程式為 std::has_unique_object_representationsstd::has_unique_object_representations_v 新增特化,則行為未定義。

目錄

[編輯] 模板引數

T - 要檢查的型別

[編輯] 輔助變數模板

template< class T >

constexpr bool has_unique_object_representations_v =

    has_unique_object_representations<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>

[編輯] 註解

引入此特性是為了判斷一個型別是否可以透過將其物件表示作為位元組陣列進行雜湊來正確地進行雜湊。

特性測試 標準 特性
__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)
雜湊函式物件
(類模板) [編輯]