首页 > 其他分享 >std::ref

std::ref

时间:2023-02-23 15:22:20浏览次数:37  
标签:std ref v1 v2 vector type

The std::thread constructor copies the supplied values, without converting to the expected argument type (which is reference type in this case, seeupdate()). So we need to wrap the arguments that really needs to be references in std::ref.

So, in std::thread the std::ref in to make in compile with correct augument type.

Also, an example where std::ref is useful 

 

std::vector<int> v1, v2;
  
void test() {
  for (std::vector<int>& vv : 
    // Compiles
    { std::ref(v1), std::ref(v2) } 
  
    // Compiler rejects this with:
    //   binding reference of type 'vector<...>' to value of 
    //   type 'const vector<...>' drops 'const' qualifier 
    // { v1, v2} 
  ) {
      vv.push_back(3);
  }

 

标签:std,ref,v1,v2,vector,type
From: https://www.cnblogs.com/selfmade-Henderson/p/17148096.html

相关文章