Skip to content

Commit a80ff2a

Browse files
committed
Add a regression test
1 parent 2f85010 commit a80ff2a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

include/function2/function2.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ struct box<false, T, Allocator> : private Allocator {
397397
T value_;
398398

399399
explicit box(const T& value, Allocator allocator_)
400-
: Allocator(std::move(allocator_)), value_(std::move(value)) {
400+
: Allocator(std::move(allocator_)), value_(value) {
401401
}
402402

403403
explicit box(T&& value, Allocator allocator_)

test/regressions.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <memory>
88
#include <string>
9+
#include <utility>
910
#include <vector>
1011

1112
#include "function2-test.hpp"
@@ -261,3 +262,43 @@ TYPED_TEST(AllReferenceRetConstructTests, reference_returns_not_buildable) {
261262
ref_obj& ref = left();
262263
ASSERT_EQ(ref.data(), 8373827);
263264
}
265+
266+
/// Functor which counts the number of times it has been copied or moved
267+
class CopyAndMoveCountFunctor {
268+
int copy_count = 0;
269+
int move_count = 0;
270+
271+
public:
272+
CopyAndMoveCountFunctor() = default;
273+
274+
CopyAndMoveCountFunctor(const CopyAndMoveCountFunctor& that) {
275+
*this = that;
276+
}
277+
278+
CopyAndMoveCountFunctor& operator=(const CopyAndMoveCountFunctor& that) {
279+
copy_count = that.copy_count + 1;
280+
move_count = that.move_count;
281+
return *this;
282+
}
283+
284+
CopyAndMoveCountFunctor(CopyAndMoveCountFunctor&& that) {
285+
*this = std::move(that);
286+
}
287+
288+
CopyAndMoveCountFunctor& operator=(CopyAndMoveCountFunctor&& that) {
289+
copy_count = that.copy_count;
290+
move_count = that.move_count + 1;
291+
return *this;
292+
}
293+
294+
~CopyAndMoveCountFunctor() = default;
295+
296+
std::pair<int, int> operator()() const {
297+
return std::make_pair(copy_count, move_count);
298+
}
299+
};
300+
301+
TEST(regression_tests, no_extra_move_during_construction) {
302+
fu2::function<std::pair<int, int>()> f(CopyAndMoveCountFunctor{});
303+
ASSERT_EQ(f(), std::make_pair(0, 1));
304+
}

0 commit comments

Comments
 (0)