std::meta::has_unique_object_representations
| Defined in header <meta>
|
||
consteval bool has_unique_object_representations( std::meta::info r );
|
(since C++26) | |
Returns true if r represents a trivially copyable type T and if any two objects of type T with the same value have the same object representation. For any other type T, returns false.
For the purpose of this meta-function, 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 meta-function, but integer types that do not use padding bits are guaranteed to have unique object representations.
If std::meta::remove_all_extents(r) represents an incomplete type other than (possibly cv-qualified) void, the behavior is undefined.
Contents
Parameters
| r | - | a reflection value |
Return value
true if r has unique object representations; otherwise false.
Notes
This meta-function can be used to determine whether a type can be correctly hashed by hashing its object representation as a byte array.
Example
#include <cstdint>
#include <meta>
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(has_unique_object_representations(^^char));
// For IEC 559 floats, assertion passes because the
// value NaN has multiple object representations.
static_assert(!has_unique_object_representations(^^float));
// Should succeed in any sane implementation because ‘unpadded’ is
// typically not padded, and std::uint32_t cannot contain padding bits.
static_assert(has_unique_object_representations(^^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(!has_unique_object_representations(^^likely_padded));
// Notable architectural divergence:
static_assert(has_unique_object_representations(^^bool)); // x86
// static_assert(!has_unique_object_representations(^^bool)); // ARM
}
See also
(C++26) |
checks if reflection represents a (scalar) value (function) |
(C++26) |
checks if reflection represents a static object (function) |
(C++26) |
checks if reflected type is an integral type (function) |
(C++26) |
checks if reflected type is a scalar type (function) |
(C++26) |
checks if reflected type is a non-union class type (function) |
(C++26) |
checks if reflected type is a union type (function) |
(C++26) |
checks if reflected type is an array type (function) |
(C++26) |
checks if reflected type is a standard-layout type (function) |
(C++11) |
hash function object (class template) |
| checks if every bit in the type's object representation contributes to its value (class template) |