保存参考
考虑以下简单的代码:
#include <thread>
#include <utility>
#include <vector>
#include <atomic>
#include <queue>
#include <iostream>
#include <functional>
using namespace std;
template<class It, class Fun>
void parallel_for(size_t num_threads, It first, It end, const Fun& fun) {
std::queue<std::thread> ts;
for (It it = first; it != end; ++it) {
if (std::distance(first, it) % num_threads == 0) {
fun(*it);
} else {
if (ts.size() == num_threads-1) {
ts.front().join();
ts.pop();
}
ts.push(std::thread(fun, std::ref(*it)));
}
}
while (not ts.empty()) {
ts.front().join();
ts.pop();
}
}
int main() {
std::atomic_int counter = 1;
auto lam = [&counter](auto& vl) {
vl = std::pair(counter++, -1);
};
// The following usage of std::ref works okay:
pair<int, int> x;
auto blam = bind(lam, ref(x));
blam();
// Nevertheless, the next line fails:
// lam(ref(x));
// As well as the next two ones:
// vector<pair<int, int>> v = {{4, 2}};
// parallel_for(thread::hardware_concurrency(), begin(v), end(v), lam);
return 0;
}
GCC在最后两行上的错误,尤其是
In file included from ./src/csc_cpp/passing_lambdas.cpp:1:
/usr/include/c++/10/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = const main()::<lambda(auto:1&)>&; _Args = {std::reference_wrapper<std::pair<int, int> >}; <template-parameter-1-3> = void]’:
./src/csc_cpp/passing_lambdas.cpp:22:26: required from ‘void parallel_for(size_t, It, It, const Fun&) [with It = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >; Fun = main()::<lambda(auto:1&)>; size_t = long unsigned int]’
./src/csc_cpp/passing_lambdas.cpp:47:71: required from here
/usr/include/c++/10/thread:136:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
136 | typename decay<_Args>::type...>::value,
| ^~~~~
我确定这是一个琐碎的问题,但是无论如何我都在努力理解这一点。我认为我一直在 std :: thread :: thread()
非常紧密地使用使用的示例,但这并没有编译。我在做什么错?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,让我澄清一下,因为我不确定它是否显而易见:
std::ref
是它返回一个std::reference_wrapper
,因此可以将结果用作对象,但该对象可以隐式转换为T&
,因此可以在需要T&
的地方进行替换。lam(ref(x));
失败,因为您在lam
中使用了auto
。编译器不知道您希望vl
成为std::pair&
,它会根据得到的内容进行推断。std::ref
返回一个临时的std::reference_wrapper>
,它不能绑定到非const参考。在 lambda 中使用显式类型并进行编译:
或者,您可以使用
get()
或static_cast 显式转换为
std::pair&
第二部分与
parallel_for
具有完全相同的问题,您将std::reference_wrapper
的右值传递给lam
。First, let me clarify, because I'm not sure if it's obvious: the trick behind
std::ref
is that it returns an object of typestd::reference_wrapper<T>
, so you can use the result as object, but the object is implicitly convertible toT&
, so it can be substituted whereT&
is needed.lam(ref(x));
fails because you useauto
inlam
. Compiler doesn't know that you wantvl
to bestd::pair<int, int>&
, it deduces from what it gets.std::ref
returns a temporary ofstd::reference_wrapper<std::pair<int, int>>
, which cannot be bound to non-const
reference. Use explicit type in lambda and it compiles:Alternatively, you can explicitly convert to
std::pair<int, int>&
usingget()
orstatic_cast
The second part with
parallel_for
has exactly the same issue, you pass rvalue ofstd::reference_wrapper
tolam
.关于
x
是lvalue
,而ref(x)
是临时reference> reference> reference_wrapper
。您无法通过lam
通过auto&amp;
中的LVALUE参考抓住临时参考。对于那条线,您可以简单地使用
About
x
is anlvalue
while theref(x)
is a temporaryreference_wrapper
. You can not grab a temporary with an lvalue reference in yourlam
throughauto&
.for that line, you can simply use