这是我试图让它工作的片段
#include <functional>
#include <stdio.h>
#include <utility>
void
bar(std::function<void(int* a)>&& aFunction)
{}
void
foo(std::function<void(int* a)>&& aFunction)
{
bar(std::forward(aFunction));
}
int
main()
{
int b = 123;
foo([b](int*) { printf("Calling \n"); });
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用 clang++ 编译给了我
? /tmp clang++ main.cpp -g -o main
main.cpp:12:7: error: no matching function for call to 'forward'
bar(std::forward(aFunction));
^~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/bits/move.h:76:5: note: candidate template ignored: couldn't infer template argument '_Tp'
forward(typename std::remove_reference<_Tp>::type& __t) noexcept
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/bits/move.h:87:5: note: candidate template ignored: couldn't infer template argument '_Tp'
forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
我不知道这实际上是否与捕获子句有关。我怎样才能使代码片段工作?
这首先不是一个用例std::forward;foo'saFunction不是通用/转发参考,因此std::forward不适用。
您只想无条件地移动 r 值引用以将其传递给bar; 将代码更改为:
bar(std::move(aFunction));
Run Code Online (Sandbox Code Playgroud)
是足以使这项工作。