|
6 | 6 |
|
7 | 7 | #include <memory> |
8 | 8 | #include <string> |
| 9 | +#include <utility> |
9 | 10 | #include <vector> |
10 | 11 |
|
11 | 12 | #include "function2-test.hpp" |
@@ -262,31 +263,41 @@ TYPED_TEST(AllReferenceRetConstructTests, reference_returns_not_buildable) { |
262 | 263 | ASSERT_EQ(ref.data(), 8373827); |
263 | 264 | } |
264 | 265 |
|
265 | | -/// Functor which counts the number of times it has been moved |
266 | | -class MoveCountFunctor { |
| 266 | +/// Functor which counts the number of times it has been copied or moved |
| 267 | +class CopyAndMoveCountFunctor { |
| 268 | + int copy_count = 0; |
267 | 269 | int move_count = 0; |
268 | 270 |
|
269 | 271 | public: |
270 | | - MoveCountFunctor() { |
| 272 | + CopyAndMoveCountFunctor() = default; |
| 273 | + |
| 274 | + CopyAndMoveCountFunctor(const CopyAndMoveCountFunctor& that) { |
| 275 | + *this = that; |
271 | 276 | } |
272 | 277 |
|
273 | | - MoveCountFunctor(const MoveCountFunctor&) = delete; |
274 | | - MoveCountFunctor& operator=(const MoveCountFunctor&) = delete; |
275 | | - ~MoveCountFunctor() = default; |
| 278 | + CopyAndMoveCountFunctor& operator=(const CopyAndMoveCountFunctor& that) { |
| 279 | + copy_count = that.copy_count + 1; |
| 280 | + move_count = that.move_count; |
| 281 | + } |
276 | 282 |
|
277 | | - MoveCountFunctor(MoveCountFunctor&& that): move_count(that.move_count + 1) { |
| 283 | + CopyAndMoveCountFunctor(CopyAndMoveCountFunctor&& that) { |
| 284 | + *this = std::move(that); |
278 | 285 | } |
279 | | - MoveCountFunctor& operator=(MoveCountFunctor&& that) { |
| 286 | + |
| 287 | + CopyAndMoveCountFunctor& operator=(CopyAndMoveCountFunctor&& that) { |
| 288 | + copy_count = that.copy_count; |
280 | 289 | move_count = that.move_count + 1; |
281 | 290 | return *this; |
282 | 291 | } |
283 | 292 |
|
284 | | - int operator()() const { |
285 | | - return move_count; |
| 293 | + ~CopyAndMoveCountFunctor() = default; |
| 294 | + |
| 295 | + std::pair<int, int> operator()() const { |
| 296 | + return std::make_pair(copy_count, move_count); |
286 | 297 | } |
287 | 298 | }; |
288 | 299 |
|
289 | 300 | TEST(regression_tests, no_extra_move_during_construction) { |
290 | | - fu2::function<int()> f(MoveCountFunctor{}); |
291 | | - ASSERT_EQ(f(), 1); |
| 301 | + fu2::function<int()> f(CopyAndMoveCountFunctor{}); |
| 302 | + ASSERT_EQ(f(), std::make_pair(0, 1)); |
292 | 303 | } |
0 commit comments