名前空間
変種
操作

std::has_unique_object_representations

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(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_representationsUnaryTypeTraitです。

T自明にコピー可能 (trivially copyable)であり、かつ、同じ値を持つ型Tの2つのオブジェクトが同じオブジェクト表現を持つ場合、メンバ定数valuetrueに等しくなります。その他の型では、valuefalseです。

この特性の目的のために、2つの配列は要素が同じ値を持つ場合に同じ値を持ち、2つの非共用体クラスは直接サブオブジェクトが同じ値を持つ場合に同じ値を持ち、2つの共用体はアクティブなメンバが同じで、そのメンバの値が同じ場合に同じ値を持ちます。

この特性を満たすスカラー型は実装定義ですが、パディングビットを使用しない(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 =

    has_unique_object_representations<T>::value;
(C++17以降)

std::integral_constant から継承

メンバ定数

value
[static]
Tが一意のオブジェクト表現を持つ場合はtrue、そうでない場合はfalse
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、value を返します。
(public member function)
operator()
(C++14)
value を返します。
(public member function)

メンバ型

定義
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()
{
    // 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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 4113 C++17 Tは不明な境界を持つ配列である可能性がある
要素型が不完全であっても
要素を要求する
型が完全であること

[編集] 関連項目

型がスタンダードレイアウト型であるかをチェックする
(クラステンプレート) [編集]
(C++11)
ハッシュ関数オブジェクト
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)