Namespaces
Variants

std:: has_unique_object_representations

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)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
has_unique_object_representations
(C++17)
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 has_unique_object_representations ;
(C++17以降)

std::has_unique_object_representations UnaryTypeTrait です。

T trivially copyable であり、かつ同じ値を持つ T 型の任意の2つのオブジェクトが同じ object representation を持つ場合、メンバ定数 value true に設定します。それ以外の型の場合、 value false となります。

この特性の目的において、2つの配列はそれらの要素が同じ値を持つ場合に同じ値を持ち、2つの非共用体クラスはそれらの直接の部分オブジェクトが同じ値を持つ場合に同じ値を持ち、2つの共用体は同じアクティブなメンバーを持ち、そのメンバーの値が同じである場合に同じ値を持つ。

この特性を満たすスカラー型は実装定義ですが、 unsigned (until C++20) パディングビットを使用しない整数型は、一意のオブジェクト表現を持つことが保証されています。

std:: remove_all_extents_t < T > が(possibly cv-qualifiedな) void 以外の不完全型である場合、動作は未定義です。

プログラムが std::has_unique_object_representations または std::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
[static]
true T が一意なオブジェクト表現を持つ場合)、 false (それ以外の場合)
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、 value を返す
(公開メンバ関数)
operator()
(C++14)
value を返す
(公開メンバ関数)

メンバ型

定義
value_type bool
type 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()
{
    // charの各値は正確に1つのオブジェクト表現に対応する
    static_assert(std::has_unique_object_representations_v<char>);
    // IEC 559浮動小数点数の場合、値NaNが複数のオブジェクト表現を持つため
    // このアサーションは成功する
    static_assert(!std::has_unique_object_representations_v<float>);
    // unpaddedは通常パディングされず、std::uint32_tはパディングビットを含まないため
    // どのまともな実装でも成功するはず
    static_assert(std::has_unique_object_representations_v<unpadded>);
    // ほとんどの実装では失敗する。stを16ビット境界に配置するために
    // データメンバcとstの間にパディングビットが挿入されるため
    static_assert(!std::has_unique_object_representations_v<likely_padded>);
    // 注目すべきアーキテクチャの差異:
    static_assert(std::has_unique_object_representations_v<bool>);  // x86
 // static_assert(!std::has_unique_object_representations_v<bool>); // ARM
}

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 4113 C++17 T は要素型が不完全であっても
未知のサイズの配列となり得た
要素型が完全であることを
要求する

関連項目

型が standard-layout 型かどうかをチェックする
(クラステンプレート)
(C++11)
ハッシュ関数オブジェクト
(クラステンプレート)