std::has_un...esentations(3)
π NAME π
std::has_unique_object_representations β std::has_unique_object_representations (yes bestie, the name repeats, itβs iconic like that π
)
Welcome to the C++ Standard Libary manual page for std::has_unique_object_representations, aka the std::has_un...esentations(3) page. This is giving deep type-trait excellence, and we are obsessed. π
β¨ Synopsis β¨
Defined in header <type_traits>:
template< class T > (since C++17)
struct has_unique_object_representations;
Okay so std::has_unique_object_representations is a UnaryTypeTrait. Slay.
If T is TriviallyCopyable and if any two objects of type T with the same value have the same object representation, it 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 ifTis an incomplete type other than (possibly cv-qualified)voidor array of unknown bound. Not me warning you dramatically, but likeβ¦ bestie, heed the warning. π
If the program adds specializations for std::has_unique_object_representations or std::has_unique_object_representations_v, the behavior is undefined. Periodt.
πΈ Template parameters πΈ
Tβ a type to check
π Helper variable template π
template< class T >
inline constexpr bool has_unique_object_representations_v = (since C++17)
has_unique_object_representations<T>::value;
π¦ Inherited from std::integral_constant π¦
Member constants
value[static] (public static member constant) βtrueifThas unique object representations,falseotherwise
Member functions
operator boolβ converts the object tobool, returnsvalue(public member function)operator()(C++14)β returnsvalue(public member function)
Member types
value_typeβbooltypeβ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. iykyk. π§
- Feature-test macro:
__cpp_lib_has_unique_object_representationsβ value201606L, Std(C++17). The feature isstd::has_unique_object_representations, obviously. π
π₯ Example π₯
// Run this code
#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
}
π See also π
is_standard_layout(C++11)β checks if a type is a standard-layout type (class template)hash(C++11)β hash function object (class template)
This lilβ page is from cppreference.com, dated 2024.06.10, and its footer vibes are std::has_un...esentations(3) β so thatβs the whole manual-page moment. Bye bestie, go check your padding bits and stay slay! π«Άπβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms