Added bx::hasUniqueObjectRepresentation type trait. (#420)

This commit is contained in:
Branimir Karadžić
2026-08-30 18:02:11 +00:00
committed by GitHub
parent 671a001d3e
commit 080f466590
3 changed files with 29 additions and 0 deletions

View File

@@ -366,6 +366,15 @@ namespace bx
return IsTriviallyCopyableT<Ty>::value;
}
//---
template<typename Ty> struct HasUniqueObjectRepresentationT : public BoolConstantT<__has_unique_object_representations(Ty)> {};
template<typename Ty>
inline constexpr bool hasUniqueObjectRepresentation()
{
return HasUniqueObjectRepresentationT<Ty>::value;
}
//---
template<typename Ty> struct IsTriviallyDestructibleT : public BoolConstantT<
#if BX_COMPILER_GCC

View File

@@ -129,6 +129,11 @@ namespace bx
template<typename Ty>
constexpr bool isTriviallyCopyable();
/// Returns true if any two objects of type `Ty` that hold the same value also have the
/// same object representation, otherwise returns false. False for a type with padding.
template<typename Ty>
constexpr bool hasUniqueObjectRepresentation();
/// Returns true if type `Ty` has trivial destructor, otherwise returns false.
template<typename Ty>
constexpr bool isTriviallyDestructible();

View File

@@ -25,6 +25,8 @@ struct TestClassDerivedB /*
: TestClassDerivedA { };
struct TestClassDerivedX /* */
: TestClassVirtualDtor { };
struct TestClassPadded { uint32_t x; uint16_t y; uint8_t z; };
struct TestClassNoPadding { uint32_t x; uint16_t y; uint8_t z; uint8_t w; };
union TestUnionEmpty { };
union TestUnion { int32_t x; float y; };
enum TestEnumEmpty { };
@@ -210,6 +212,19 @@ TEST_CASE("type-traits isTriviallyCopyable", "")
STATIC_REQUIRE(!bx::isTriviallyCopyable<TestClassVirtualDtor >() );
}
TEST_CASE("type-traits hasUniqueObjectRepresentation", "")
{
STATIC_REQUIRE( 8 == sizeof(TestClassPadded) );
STATIC_REQUIRE( 8 == sizeof(TestClassNoPadding) );
STATIC_REQUIRE( bx::hasUniqueObjectRepresentation<int32_t >() );
STATIC_REQUIRE( bx::hasUniqueObjectRepresentation<TestClassMember >() );
STATIC_REQUIRE( bx::hasUniqueObjectRepresentation<TestClassNoPadding >() );
STATIC_REQUIRE(!bx::hasUniqueObjectRepresentation<TestClassPadded >() );
STATIC_REQUIRE(!bx::hasUniqueObjectRepresentation<TestClassVirtualDtor >() );
STATIC_REQUIRE(!bx::hasUniqueObjectRepresentation<float >() );
}
TEST_CASE("type-traits isTriviallyConstructible", "")
{
STATIC_REQUIRE( bx::isTriviallyConstructible<int32_t >() );