为什么以下代码(在Ideone上)会给我一个错误?
输出:
然而,如果我在参数列表中将
#include <future>
#include <iostream>
#include <string>
int main()
{
int foo = 0;
bool bar = false;
std::future<std::string> async_request = std::async(
std::launch::async,
[=, &foo](bool& is_pumping_request) -> std::string {
return "str";
},
bar
);
std::cout << async_request.get() << std::endl;
}
输出:
In file included from /usr/include/c++/5/future:38:0,
from prog.cpp:1:
/usr/include/c++/5/functional: In instantiation of 'struct std::_Bind_simple<main()::<lambda(bool&)>(bool)>':
/usr/include/c++/5/future:1709:67: required from 'std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = main()::<lambda(bool&)>; _Args = {bool&}; typename std::result_of<_Functor(_ArgTypes ...)>::type = std::basic_string<char>]'
prog.cpp:15:2: required from here
/usr/include/c++/5/functional:1505:61: error: no type named 'type' in 'class std::result_of<main()::<lambda(bool&)>(bool)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/5/functional:1526:9: error: no type named 'type' in 'class std::result_of<main()::<lambda(bool&)>(bool)>'
_M_invoke(_Index_tuple<_Indices...>)
^
然而,如果我在参数列表中将
bool&
更改为bool
,它会成功编译。为什么呢?
cref
是一个好的编程实践,但是ref
甚至不传递任何参数(传递一个副本)仍然可以工作(根据“工作”的定义)。 - T.C.