Explorar o código

any: better support to rvalue references for forward_as_any

Michele Caini %!s(int64=3) %!d(string=hai) anos
pai
achega
b16cefd534
Modificáronse 2 ficheiros con 12 adicións e 11 borrados
  1. 11 10
      src/entt/core/any.hpp
  2. 1 1
      test/entt/core/any.cpp

+ 11 - 10
src/entt/core/any.hpp

@@ -122,26 +122,27 @@ class basic_any {
 
 
     template<typename Type, typename... Args>
     template<typename Type, typename... Args>
     void initialize([[maybe_unused]] Args &&...args) {
     void initialize([[maybe_unused]] Args &&...args) {
-        info = &type_id<std::remove_cv_t<std::remove_reference_t<Type>>>();
+        using basic_type = std::remove_cv_t<std::remove_reference_t<Type>>;
+        info = &type_id<basic_type>();
 
 
         if constexpr(!std::is_void_v<Type>) {
         if constexpr(!std::is_void_v<Type>) {
-            vtable = basic_vtable<std::remove_cv_t<std::remove_reference_t<Type>>>;
+            vtable = basic_vtable<basic_type>;
 
 
             if constexpr(std::is_lvalue_reference_v<Type>) {
             if constexpr(std::is_lvalue_reference_v<Type>) {
                 static_assert(sizeof...(Args) == 1u && (std::is_lvalue_reference_v<Args> && ...), "Invalid arguments");
                 static_assert(sizeof...(Args) == 1u && (std::is_lvalue_reference_v<Args> && ...), "Invalid arguments");
                 mode = std::is_const_v<std::remove_reference_t<Type>> ? policy::cref : policy::ref;
                 mode = std::is_const_v<std::remove_reference_t<Type>> ? policy::cref : policy::ref;
                 instance = (std::addressof(args), ...);
                 instance = (std::addressof(args), ...);
-            } else if constexpr(in_situ<Type>) {
-                if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
-                    new(&storage) Type{std::forward<Args>(args)...};
+            } else if constexpr(in_situ<basic_type>) {
+                if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<basic_type>) {
+                    new(&storage) basic_type{std::forward<Args>(args)...};
                 } else {
                 } else {
-                    new(&storage) Type(std::forward<Args>(args)...);
+                    new(&storage) basic_type(std::forward<Args>(args)...);
                 }
                 }
             } else {
             } else {
-                if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
-                    instance = new Type{std::forward<Args>(args)...};
+                if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<basic_type>) {
+                    instance = new basic_type{std::forward<Args>(args)...};
                 } else {
                 } else {
-                    instance = new Type(std::forward<Args>(args)...);
+                    instance = new basic_type(std::forward<Args>(args)...);
                 }
                 }
             }
             }
         }
         }
@@ -500,7 +501,7 @@ basic_any<Len, Align> make_any(Args &&...args) {
  */
  */
 template<std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename Type>
 template<std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename Type>
 basic_any<Len, Align> forward_as_any(Type &&value) {
 basic_any<Len, Align> forward_as_any(Type &&value) {
-    return basic_any<Len, Align>{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
+    return basic_any<Len, Align>{std::in_place_type<Type>, std::forward<Type>(value)};
 }
 }
 
 
 } // namespace entt
 } // namespace entt

+ 1 - 1
test/entt/core/any.cpp

@@ -1238,9 +1238,9 @@ TEST_F(Any, MakeAny) {
 
 
 TEST_F(Any, ForwardAsAny) {
 TEST_F(Any, ForwardAsAny) {
     int value = 42;
     int value = 42;
-    auto any = entt::forward_as_any(std::move(value));
     auto ref = entt::forward_as_any(value);
     auto ref = entt::forward_as_any(value);
     auto cref = entt::forward_as_any(std::as_const(value));
     auto cref = entt::forward_as_any(std::as_const(value));
+    auto any = entt::forward_as_any(std::move(value));
 
 
     ASSERT_TRUE(any);
     ASSERT_TRUE(any);
     ASSERT_TRUE(ref);
     ASSERT_TRUE(ref);