Skip to content

Commit 9c6c06c

Browse files
committed
Use move_from for lambda always
1 parent 9b12234 commit 9c6c06c

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,8 +1252,7 @@ const clang::LambdaCapture *AsLambdaCapture(const clang::FieldDecl *field) {
12521252

12531253
bool IsLambdaCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor) {
12541254
auto decl = ctor->getParent();
1255-
return ctor->isCopyOrMoveConstructor() && decl->isLambda() &&
1256-
!decl->captures().empty();
1255+
return ctor->isCopyOrMoveConstructor() && decl->isLambda();
12571256
}
12581257

12591258
bool LambdaNeedsDestruction(const clang::CXXRecordDecl *decl) {

‎tests/unit/lambda_as_field.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
22
// no-compile
33
#include <assert.h>
4+
#include <utility>
45

56
template <typename F> struct Guard {
67
F f;
@@ -16,6 +17,13 @@ template <typename F> struct Holder {
1617
}
1718
};
1819

20+
template <typename F> struct Moving {
21+
F f;
22+
int moves = 0;
23+
Moving(F f) : f(f) {}
24+
Moving(Moving &&o) : f(std::move(o.f)), moves(o.moves + 1) {}
25+
};
26+
1927
template <typename T> auto wrap(T fn) {
2028
return [fn](int x) { return fn(x) + 1; };
2129
}
@@ -54,5 +62,10 @@ int main() {
5462
auto ww = wrap(w);
5563
assert(ww(2) == 202);
5664

65+
Moving m{[](int x) { return x * 2; }};
66+
Moving m2(std::move(m));
67+
assert(m2.f(3) == 6);
68+
assert(m2.moves == 1);
69+
5770
return 0;
5871
}

0 commit comments

Comments
 (0)