diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 5766331d76..fb65d4ee06 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -929,8 +929,7 @@ class basic_registry { template [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entt) { if constexpr(sizeof...(Type) == 1u) { - auto &cpool = assure...>(); - return (static_cast(cpool.contains(entt) ? std::addressof(cpool.get(entt)) : nullptr), ...); + return (const_cast(std::as_const(*this).template try_get(entt)), ...); } else { return std::make_tuple(try_get(entt)...); } diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 18003d172a..bbee5c4216 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -1893,6 +1893,23 @@ TEST(Registry, GetOrEmplace) { ASSERT_EQ(registry.get(entity), 3); } +TEST(Registry, TryGet) { + entt::registry registry; + const auto entity = registry.create(); + + ASSERT_EQ(registry.try_get(entity), nullptr); + ASSERT_EQ(std::as_const(registry).try_get(entity), nullptr); + + ASSERT_EQ(std::as_const(registry).storage(), nullptr); + + const int &elem = registry.emplace(entity); + + ASSERT_NE(std::as_const(registry).storage(), nullptr); + + ASSERT_EQ(registry.try_get(entity), &elem); + ASSERT_EQ(std::as_const(registry).try_get(entity), &elem); +} + TEST(Registry, Constness) { entt::registry registry;