Browse Source

any:
* strong exception guarantee applies
* a test to avoid future regressions

Michele Caini 4 năm trước cách đây
mục cha
commit
cdd029853b
3 tập tin đã thay đổi với 21 bổ sung3 xóa
  1. 1 0
      TODO
  2. 3 3
      src/entt/core/any.hpp
  3. 17 0
      test/entt/core/any.cpp

+ 1 - 0
TODO

@@ -5,6 +5,7 @@
 
 WIP:
 * uses-allocator construction: dense map, compressed pair, any (with allocator support), cache, dispatcher, poly, ...
+* add an ENTT_NOEXCEPT with args and use it to make ie compressed_pair conditionally noexcept
 * process scheduler: reviews, use free lists internally
 * runtime events (emitter)
 * iterator based try_emplace vs try_insert for perf reasons

+ 3 - 3
src/entt/core/any.hpp

@@ -140,7 +140,7 @@ public:
     static constexpr auto alignment = Align;
 
     /*! @brief Default constructor. */
-    basic_any() ENTT_NOEXCEPT
+    constexpr basic_any() ENTT_NOEXCEPT
         : instance{},
           info{&type_id<void>()},
           vtable{},
@@ -184,7 +184,7 @@ public:
      * @brief Move constructor.
      * @param other The instance to move from.
      */
-    basic_any(basic_any &&other)
+    basic_any(basic_any &&other) ENTT_NOEXCEPT
         : instance{},
           info{other.info},
           vtable{other.vtable},
@@ -221,7 +221,7 @@ public:
      * @param other The instance to move from.
      * @return This any object.
      */
-    basic_any &operator=(basic_any &&other) {
+    basic_any &operator=(basic_any &&other) ENTT_NOEXCEPT {
         reset();
 
         if(other.vtable) {

+ 17 - 0
test/entt/core/any.cpp

@@ -1293,6 +1293,23 @@ TEST_F(Any, NotCopyableType) {
     ASSERT_TRUE(copy.owner());
 }
 
+TEST_F(Any, NotCopyableValueType) {
+    std::vector<entt::any> vec{};
+    vec.emplace_back(std::in_place_type<not_copyable>);
+    vec.shrink_to_fit();
+
+    ASSERT_EQ(vec.size(), 1u);
+    ASSERT_EQ(vec.capacity(), 1u);
+    ASSERT_TRUE(vec[0u]);
+
+    // strong exception guarantee due to noexcept move ctor
+    vec.emplace_back(std::in_place_type<not_copyable>);
+
+    ASSERT_EQ(vec.size(), 2u);
+    ASSERT_TRUE(vec[0u]);
+    ASSERT_TRUE(vec[1u]);
+}
+
 TEST_F(Any, NotMovableType) {
     entt::any any{std::in_place_type<not_movable>};
     entt::any other{std::in_place_type<not_movable>};