Skip to content

Commit cfb7cd5

Browse files
committed
[shared_func] support free funcs
1 parent 0e2ae2f commit cfb7cd5

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

include/itlib/shared_func.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class shared_func<R(Args...)> {
123123
template <typename FO>
124124
struct wrapper : public wrapper_base {
125125
FO func_object;
126-
wrapper(FO&& f) : func_object(std::forward<FO>(f)) {
126+
wrapper(FO f) : func_object(std::move(f)) {
127127
this->func = +[](void* obj, Args&&... args) -> R {
128128
return static_cast<wrapper*>(obj)->func_object(std::forward<Args>(args)...);
129129
};
@@ -144,7 +144,7 @@ class shared_func<R(Args...)> {
144144
if (!f) {
145145
return {};
146146
}
147-
return std::make_shared<wrapper<FP>>(f);
147+
return std::make_shared<wrapper<FP*>>(f);
148148
}
149149
};
150150

test/t-shared_func-11.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ TEST_CASE("lambda") {
5555
CHECK(func.use_count() == 2);
5656
}
5757
}
58+
59+
int sum(int a, int b) { return a + b; }
60+
61+
TEST_CASE("free func") {
62+
itlib::shared_func<int(int, int)> func = sum;
63+
CHECK(func(1, 2) == 3);
64+
func = [](int a, int b) { return a * b; };
65+
CHECK(func(3, 4) == 12);
66+
func = sum;
67+
CHECK(func(3, 4) == 7);
68+
}

0 commit comments

Comments
 (0)