From 080f466590c161664f2a3c5d21e05bd31cc9c8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 30 Aug 2026 18:02:11 +0000 Subject: [PATCH] Added bx::hasUniqueObjectRepresentation type trait. (#420) --- include/bx/inline/typetraits.inl | 9 +++++++++ include/bx/typetraits.h | 5 +++++ tests/typetraits_test.cpp | 15 +++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/include/bx/inline/typetraits.inl b/include/bx/inline/typetraits.inl index a6ba3e6..5b7a2b2 100644 --- a/include/bx/inline/typetraits.inl +++ b/include/bx/inline/typetraits.inl @@ -366,6 +366,15 @@ namespace bx return IsTriviallyCopyableT::value; } + //--- + template struct HasUniqueObjectRepresentationT : public BoolConstantT<__has_unique_object_representations(Ty)> {}; + + template + inline constexpr bool hasUniqueObjectRepresentation() + { + return HasUniqueObjectRepresentationT::value; + } + //--- template struct IsTriviallyDestructibleT : public BoolConstantT< #if BX_COMPILER_GCC diff --git a/include/bx/typetraits.h b/include/bx/typetraits.h index 0c8b160..c40ffee 100644 --- a/include/bx/typetraits.h +++ b/include/bx/typetraits.h @@ -129,6 +129,11 @@ namespace bx template 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 + constexpr bool hasUniqueObjectRepresentation(); + /// Returns true if type `Ty` has trivial destructor, otherwise returns false. template constexpr bool isTriviallyDestructible(); diff --git a/tests/typetraits_test.cpp b/tests/typetraits_test.cpp index 8ded209..ce2f74d 100644 --- a/tests/typetraits_test.cpp +++ b/tests/typetraits_test.cpp @@ -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() ); } +TEST_CASE("type-traits hasUniqueObjectRepresentation", "") +{ + STATIC_REQUIRE( 8 == sizeof(TestClassPadded) ); + STATIC_REQUIRE( 8 == sizeof(TestClassNoPadding) ); + + STATIC_REQUIRE( bx::hasUniqueObjectRepresentation() ); + STATIC_REQUIRE( bx::hasUniqueObjectRepresentation() ); + STATIC_REQUIRE( bx::hasUniqueObjectRepresentation() ); + STATIC_REQUIRE(!bx::hasUniqueObjectRepresentation() ); + STATIC_REQUIRE(!bx::hasUniqueObjectRepresentation() ); + STATIC_REQUIRE(!bx::hasUniqueObjectRepresentation() ); +} + TEST_CASE("type-traits isTriviallyConstructible", "") { STATIC_REQUIRE( bx::isTriviallyConstructible() );