Skip to content

Commit 5bca8f2

Browse files
authored
[orc-rt] Allow move_only_function to capture by lvalue-reference. (#155716)
Store std::decay_t<Callable> to ensure that we can initialize via lvalue references: auto NamedNoop = [](){}; move_only_function<void()> Noop(NamedNoop); // <- no longer an error!
1 parent 6768056 commit 5bca8f2

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

orc-rt/include/orc-rt/move_only_function.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define ORC_RT_MOVE_ONLY_FUNCTION_H
2727

2828
#include <memory>
29+
#include <type_traits>
2930

3031
namespace orc_rt {
3132

@@ -46,7 +47,7 @@ class CallableImpl : public Callable<RetT, ArgTs...> {
4647
}
4748

4849
private:
49-
CallableT Callable;
50+
std::decay_t<CallableT> Callable;
5051
};
5152

5253
} // namespace move_only_function_detail

orc-rt/unittests/move_only_function-test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ TEST(MoveOnlyFunctionTest, Captures) {
9898
EXPECT_EQ(C5(), 15);
9999
Tmp = std::move(C5);
100100
EXPECT_EQ(Tmp(), 15);
101+
102+
// Test capture via lvalue.
103+
auto Inc = [](int N) { return N + 1; };
104+
move_only_function<int(int)> C6(Inc);
105+
EXPECT_EQ(C6(1), 2);
101106
}
102107

103108
TEST(MoveOnlyFunctionTest, MoveOnly) {

0 commit comments

Comments
 (0)