std::has_unique_object_representations

From cppreference.com
< cpp‎ | types
 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++14)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(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
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)
(C++17)

(C++11)(until C++20*)(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 
Defined in header <type_traits>
template< class T >
struct has_unique_object_representations;
(since C++17)

std::has_unique_object_representations is an UnaryTypeTrait.

If T is TriviallyCopyable and if any two objects of type T with the same value have the same object representation, provides the member constant value equal true. For any other type, value is false.

For the purpose of this trait, two arrays have the same value if their elements have the same values, two non-union classes have the same value if their direct subobjects have the same value, and two unions have the same value if they have the same active member and the value of that member is the same.

It is implementation-defined which scalar types satisfy this trait, but unsigned (until C++20) integer types that do not use padding bits are guaranteed to have unique object representations.

The behavior is undefined if T is an incomplete type other than (possibly cv-qualified) void or array of unknown bound.

The behavior of a program that adds specializations for std::has_unique_object_representations or std::has_unique_object_representations_v is undefined.

Template parameters

T - a type to check

Helper variable template

template< class T >

inline constexpr bool has_unique_object_representations_v =

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

Inherited from std::integral_constant

Member constants

value
[static]
true if T has unique object representations, false otherwise
(public static member constant)

Member functions

operator bool
converts the object to bool, returns value
(public member function)
operator()
(C++14)
returns value
(public member function)

Member types

Type Definition
value_type bool
type std::integral_constant<bool, value>

Notes

This trait was introduced to make it possible to determine whether a type can be correctly hashed by hashing its object representation as a byte array.

Feature-test macro Value Std Comment
__cpp_lib_has_unique_object_representations 201606L (C++17) std::has_unique_object_representations

Example

#include <cstdint>
#include <type_traits>
 
struct bar
{
    std::uint32_t a, b;
};
 
struct foo
{
    std::uint8_t c;
    std::uint16_t st;
    std::uint32_t i;
};
 
int main()
{
    static_assert(std::has_unique_object_representations_v<char>);
    static_assert(std::has_unique_object_representations_v<bar>);
    static_assert(!std::has_unique_object_representations_v<foo>);
 
    // Notable architectural divergence:
    static_assert(std::has_unique_object_representations_v<bool>); // x86
    // static_assert(!std::has_unique_object_representations_v<bool>); // ARM
}

See also

checks if a type is a standard-layout type
(class template)
(C++11)
hash function object
(class template)