考虑以下代码:链接:
#include <iostream>
#include <thread>
int main() {
std::thread t;
const auto l = [x = std::move(t)]{};
decltype(l) m = std::move(l);
}
这段代码无法编译,出现以下错误信息:
prog.cc: In function 'int main()':
prog.cc:7:32: error: use of deleted function 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)'
7 | decltype(l) m = std::move(l);
| ^
prog.cc:6:37: note: 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)' is implicitly deleted because the default definition would be ill-formed:
6 | const auto l = [x = std::move(t)]{};
| ^
prog.cc:6:37: error: use of deleted function 'std::thread::thread(const std::thread&)'
In file included from prog.cc:2:
/opt/wandbox/gcc-head/include/c++/10.0.1/thread:154:5: note: declared here
154 | thread(const thread&) = delete;
| ^~~~~~
有没有一种方法可以使 lambda 函数在不显式捕获任何不可复制变量的情况下,成为不可复制或不可移动的?(即将 []
留空)
NoCopyMove() = default;
。 - αλεχολυτ