diff --git a/src/entt/core/compressed_pair.hpp b/src/entt/core/compressed_pair.hpp index 202425cac..090b0007c 100644 --- a/src/entt/core/compressed_pair.hpp +++ b/src/entt/core/compressed_pair.hpp @@ -209,6 +209,33 @@ public: swap(first(), other.first()); swap(second(), other.second()); } + + /** + * @brief Extracts an element from the compressed pair. + * @tparam Index An integer value that is either 0 or 1. + * @return Returns a reference to the first element if `Index` is 0 and a + * reference to the second element if `Index` is 1. + */ + template + decltype(auto) get() ENTT_NOEXCEPT { + if constexpr(Index == 0u) { + return first(); + } else { + static_assert(Index == 1u, "Index out of bounds"); + return second(); + } + } + + /*! @copydoc get */ + template + decltype(auto) get() const ENTT_NOEXCEPT { + if constexpr(Index == 0u) { + return first(); + } else { + static_assert(Index == 1u, "Index out of bounds"); + return second(); + } + } }; @@ -238,4 +265,15 @@ inline void swap(compressed_pair &lhs, compressed_pair + struct tuple_size>: integral_constant {}; + + template + struct tuple_element>: conditional { + static_assert(Index < 2u, "Index out of bounds"); + }; +} + + #endif diff --git a/test/entt/core/compressed_pair.cpp b/test/entt/core/compressed_pair.cpp index 26b7e83e9..62553c6b4 100644 --- a/test/entt/core/compressed_pair.cpp +++ b/test/entt/core/compressed_pair.cpp @@ -130,3 +130,40 @@ TEST(CompressedPair, Swap) { ASSERT_EQ(other.first(), 3); ASSERT_EQ(other.second(), 4); } + +TEST(CompressedPair, Get) { + entt::compressed_pair pair{1, 2}; + + ASSERT_EQ(pair.get<0>(), 1); + ASSERT_EQ(pair.get<1>(), 2); + + ASSERT_EQ(&pair.get<0>(), &pair.first()); + ASSERT_EQ(&pair.get<1>(), &pair.second()); + + auto &&[first, second] = pair; + + ASSERT_EQ(first, 1); + ASSERT_EQ(second, 2); + + first = 3; + second = 4; + + ASSERT_EQ(pair.first(), 3); + ASSERT_EQ(pair.second(), 4); + + auto &[cfirst, csecond] = std::as_const(pair); + + ASSERT_EQ(cfirst, 3); + ASSERT_EQ(csecond, 4); + + static_assert(std::is_same_v); + static_assert(std::is_same_v); + + auto [tfirst, tsecond] = entt::compressed_pair{9, 99}; + + ASSERT_EQ(tfirst, 9); + ASSERT_EQ(tsecond, 99); + + static_assert(std::is_same_v); + static_assert(std::is_same_v); +}