类型特性
类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。
试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。
定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。
类型属性
检查是否该类型对象的每一位都对其值有贡献
std::has_unique_object_representations
定义于头文件 | ||
template< class T > | (C++17 起) |
若 T
为可平凡复制 (TriviallyCopyable) ,且若任意二个拥有相同值的 T
类型对象拥有相同的对象表示,则提供等于 true 的成员常量 value
。对于任何其他类型, value
为 false 。
为此特性的目的,若二个数组的元素拥有相同值,则数组拥有相同值,若二个非联合体类的子对象拥有相同值,则它们拥有相同值,而若二个联合体拥有相同的活跃成员,且该成员值相同,则它们拥有相同值。
标量类型是否满足此特性是实现定义的,但保证不使用填充位的无符号 (C++20 前)整数类型拥有唯一对象表示。
若 T
为 void (可以有 cv 限定)或未知边界数组外的不完整类型,则行为未定义。
模板形参
T | - | 要检查的类型 |
辅助变量模板
template< class T > | (C++17 起) |
继承自 std::integral_constant
成员常量
value [静态] | 若 T 拥有唯一对象表示则为 true ,否则为 false(公开静态成员常量) |
成员函数
operator bool | 转换对象为 bool ,返回 value (公开成员函数) |
operator() (C++14) | 返回 value (公开成员函数) |
成员类型
类型 | 定义 |
value_type | bool |
type | std::integral_constant<bool, value> |
注意
引入此特性,是为了能够确定是否能通过哈希一个类型的对象表示,正确地哈希该类型。
调用示例
#include <iostream>
#include <type_traits>
namespace std
{
template <typename T>
struct has_unique_object_representations
{
static const bool value = false; // 默认情况下,假设没有唯一的对象表示
};
template <>
struct has_unique_object_representations<void>
{
static const bool value = true; // void 类型总是具有唯一的对象表示
};
template <typename T>
struct has_unique_object_representations<T*>
{
static const bool value = true; // 指针类型总是具有唯一的对象表示
};
template <typename T>
struct has_unique_object_representations<T&>
{
static const bool value = true; // 指针类型总是具有唯一的对象表示
};
template <typename T>
struct has_unique_object_representations<const T*>
{
static const bool value = true; // 常量指针类型也总是具有唯一的对象表示
};
template <class T>
using has_unique_object_representations_t = typename has_unique_object_representations<T>::value;
//template <typename T>
//struct has_unique_object_representations<T>
//{
// static const bool value = std::is_trivially_copyable<T>::value &&
// !std::is_reference<T>::value &&
// !std::is_volatile<T>::value &&
// !std::is_const<T>::value;
//};
}
class E
{
public:
template<class T> E(T&&) { }
};
class A {};
class B : public A {};
class C {};
class D
{
public:
operator C()
{
return c;
} C c;
};
int main()
{
std::cout << std::boolalpha;
std::cout << "std::has_unique_object_representations<A>::value : "
<< std::has_unique_object_representations<A>::value << std::endl;
std::cout << "std::has_unique_object_representations<B>::value : "
<< std::has_unique_object_representations<B>::value << std::endl;
std::cout << "std::has_unique_object_representations<C>::value : "
<< std::has_unique_object_representations<C>::value << std::endl;
std::cout << "std::has_unique_object_representations<D>::value : "
<< std::has_unique_object_representations<D>::value << std::endl;
std::cout << "std::has_unique_object_representations<E>::value : "
<< std::has_unique_object_representations<E>::value << std::endl;
std::cout << "std::has_unique_object_representations<float>::value : "
<< std::has_unique_object_representations<float>::value << std::endl;
std::cout << "std::has_unique_object_representations<int>::value : "
<< std::has_unique_object_representations<int>::value << std::endl;
std::cout << "std::has_unique_object_representations<bool>::value : "
<< std::has_unique_object_representations<bool>::value << std::endl;
std::cout << "std::has_unique_object_representations<char>::value : "
<< std::has_unique_object_representations<char>::value << std::endl;
std::cout << "std::has_unique_object_representations<viod>::value : "
<< std::has_unique_object_representations<void>::value << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << "std::has_unique_object_representations<A*>::value : "
<< std::has_unique_object_representations<A*>::value << std::endl;
std::cout << "std::has_unique_object_representations<B*>::value : "
<< std::has_unique_object_representations<B*>::value << std::endl;
std::cout << "std::has_unique_object_representations<C*>::value : "
<< std::has_unique_object_representations<C*>::value << std::endl;
std::cout << "std::has_unique_object_representations<D*>::value : "
<< std::has_unique_object_representations<D*>::value << std::endl;
std::cout << "std::has_unique_object_representations<E*>::value : "
<< std::has_unique_object_representations<E*>::value << std::endl;
std::cout << "std::has_unique_object_representations<float*>::value : "
<< std::has_unique_object_representations<float*>::value << std::endl;
std::cout << "std::has_unique_object_representations<int*>::value : "
<< std::has_unique_object_representations<int*>::value << std::endl;
std::cout << "std::has_unique_object_representations<bool*>::value : "
<< std::has_unique_object_representations<bool*>::value << std::endl;
std::cout << "std::has_unique_object_representations<char*>::value : "
<< std::has_unique_object_representations<char*>::value << std::endl;
std::cout << "std::has_unique_object_representations<viod*>::value : "
<< std::has_unique_object_representations<void*>::value << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << "std::has_unique_object_representations<A&>::value : "
<< std::has_unique_object_representations<A&>::value << std::endl;
std::cout << "std::has_unique_object_representations<B&>::value : "
<< std::has_unique_object_representations<B&>::value << std::endl;
std::cout << "std::has_unique_object_representations<C&>::value : "
<< std::has_unique_object_representations<C&>::value << std::endl;
std::cout << "std::has_unique_object_representations<D&>::value : "
<< std::has_unique_object_representations<D&>::value << std::endl;
std::cout << "std::has_unique_object_representations<E&>::value : "
<< std::has_unique_object_representations<E&>::value << std::endl;
std::cout << "std::has_unique_object_representations<float&>::value : "
<< std::has_unique_object_representations<float&>::value << std::endl;
std::cout << "std::has_unique_object_representations<int&>::value : "
<< std::has_unique_object_representations<int&>::value << std::endl;
std::cout << "std::has_unique_object_representations<bool&>::value : "
<< std::has_unique_object_representations<bool&>::value << std::endl;
std::cout << "std::has_unique_object_representations<char&>::value : "
<< std::has_unique_object_representations<char&>::value << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
return 0;
}
输出
std::has_unique_object_representations<A>::value : false
std::has_unique_object_representations<B>::value : false
std::has_unique_object_representations<C>::value : false
std::has_unique_object_representations<D>::value : false
std::has_unique_object_representations<E>::value : false
std::has_unique_object_representations<float>::value : false
std::has_unique_object_representations<int>::value : false
std::has_unique_object_representations<bool>::value : false
std::has_unique_object_representations<char>::value : false
std::has_unique_object_representations<viod>::value : true
-----------------------------------------------
std::has_unique_object_representations<A*>::value : true
std::has_unique_object_representations<B*>::value : true
std::has_unique_object_representations<C*>::value : true
std::has_unique_object_representations<D*>::value : true
std::has_unique_object_representations<E*>::value : true
std::has_unique_object_representations<float*>::value : true
std::has_unique_object_representations<int*>::value : true
std::has_unique_object_representations<bool*>::value : true
std::has_unique_object_representations<char*>::value : true
std::has_unique_object_representations<viod*>::value : true
-----------------------------------------------
std::has_unique_object_representations<A&>::value : true
std::has_unique_object_representations<B&>::value : true
std::has_unique_object_representations<C&>::value : true
std::has_unique_object_representations<D&>::value : true
std::has_unique_object_representations<E&>::value : true
std::has_unique_object_representations<float&>::value : true
std::has_unique_object_representations<int&>::value : true
std::has_unique_object_representations<bool&>::value : true
std::has_unique_object_representations<char&>::value : true
-----------------------------------------------