std::thread
作为参数调用函数func()
的arg
:#include <thread>
#include <iostream>
struct foo {
foo() = default;
foo(const foo&) { std::cout << "copy ctor" << std::endl; }
foo(foo&&) noexcept { std::cout << "move ctor" << std::endl; }
};
void func(foo){}
int main() {
foo arg;
std::thread th(func, arg);
th.join();
}
我的输出是
copy ctor
move ctor
move ctor
据我所知,和一个动作结构。为什么要进行第二步构造?arg
是在线程对象内部复制的,然后作为右值(移动)传递给func()
。因此,我希望一个副本结构
考虑该程序,该程序实质上创建了以arg为参数调用函数func()的std :: thread:#include
#include
func
按值使用其参数。更改为任一void func(const foo &){}