Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/function2/function2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,11 @@ struct box : private Allocator {

T value_;

explicit box(T value, Allocator allocator_)
explicit box(const T& value, Allocator allocator_)
: Allocator(std::move(allocator_)), value_(value) {
}

explicit box(T&& value, Allocator allocator_)
: Allocator(std::move(allocator_)), value_(std::move(value)) {
}

Expand All @@ -392,7 +396,11 @@ struct box<false, T, Allocator> : private Allocator {

T value_;

explicit box(T value, Allocator allocator_)
explicit box(const T& value, Allocator allocator_)
: Allocator(std::move(allocator_)), value_(value) {
}

explicit box(T&& value, Allocator allocator_)
: Allocator(std::move(allocator_)), value_(std::move(value)) {
}

Expand Down
41 changes: 41 additions & 0 deletions test/regressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "function2-test.hpp"
Expand Down Expand Up @@ -261,3 +262,43 @@ TYPED_TEST(AllReferenceRetConstructTests, reference_returns_not_buildable) {
ref_obj& ref = left();
ASSERT_EQ(ref.data(), 8373827);
}

/// Functor which counts the number of times it has been copied or moved
class CopyAndMoveCountFunctor {
int copy_count = 0;
int move_count = 0;

public:
CopyAndMoveCountFunctor() = default;

CopyAndMoveCountFunctor(const CopyAndMoveCountFunctor& that) {
*this = that;
}

CopyAndMoveCountFunctor& operator=(const CopyAndMoveCountFunctor& that) {
copy_count = that.copy_count + 1;
move_count = that.move_count;
return *this;
}

CopyAndMoveCountFunctor(CopyAndMoveCountFunctor&& that) {
*this = std::move(that);
}

CopyAndMoveCountFunctor& operator=(CopyAndMoveCountFunctor&& that) {
copy_count = that.copy_count;
move_count = that.move_count + 1;
return *this;
}

~CopyAndMoveCountFunctor() = default;

std::pair<int, int> operator()() const {
return std::make_pair(copy_count, move_count);
}
};

TEST(regression_tests, no_extra_move_during_construction) {
fu2::function<std::pair<int, int>()> f(CopyAndMoveCountFunctor{});
ASSERT_EQ(f(), std::make_pair(0, 2));
}