命名空间
变体
操作

std::has_unique_object_representations

来自 cppreference.cn
< 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_representations 是一个 UnaryTypeTrait

如果 T可平凡复制 (trivially copyable) 的,且若类型 T 的任何两个具有相同值的对象拥有相同的对象表示,则提供成员常量 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
[静态]
trueT 拥有唯一对象表示,否则为 false
(公有静态成员常量)

成员函数

operator bool
将对象转换为 bool,返回 value
(公有成员函数)
operator()
(C++14)
返回 value
(公有成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

[编辑] 注解

引入此特征是为了可能确定一个类型是否可以通过哈希其对象表示为字节数组来正确地哈希。

特性测试 Std 特性
__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 可以是未知边界的数组
即使其元素类型是不完整的
要求元素
类型是完整的

[编辑] 参见

检查类型是否为 标准布局 (standard-layout) 类型
(类模板) [编辑]
(C++11)
hash 函数对象
(类模板) [编辑]