C++ Thread taking reference argument failed compile

前端 未结 2 880
执念已碎
执念已碎 2020-12-16 03:08
#include
#include
using namespace std;

void f1(double& ret) {
   ret=5.;
}

int main() {
   double ret=0.;
   thread t1(f1, ret);
         


        
2条回答
  •  我在风中等你
    2020-12-16 03:41

    Jonathan's answer is definitive. Time spent studying it would be time well spent.

    In the meantime, modifying the code thus will do what you want - namely send a reference into the thread function:

    thread t1(f1, std::ref(ret));
    

提交回复
热议问题