如何使用通用lambda來創建線程並將自動參數定義爲引用?從通用lambdas創建線程並將引用作爲通用參數
舉例來說,這將是實現概念上等同於這個事情的正確方法:
int vi = 0;
auto lambda = [](auto &v) {};
auto t = std::thread(lambda, std::ref(vi));
GCC-5.3抱怨,因爲缺少類型:
/opt/gcc/el6/gcc-5.3.0/include/c++/5.3.0/functional: In instantiation of ‘struct std::_Bind_simple<main()::<lambda(auto:2&)>(std::reference_wrapper<int>)>’:
/opt/gcc/el6/gcc-5.3.0/include/c++/5.3.0/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::<lambda(auto:2&)>&; _Args = {std::reference_wrapper<int>}]’
testLambdaCapture.cpp:52:41: required from here
/opt/gcc/el6/gcc-5.3.0/include/c++/5.3.0/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(auto:2&)>(std::reference_wrapper<int>)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/opt/gcc/el6/gcc-5.3.0/include/c++/5.3.0/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(auto:2&)>(std::reference_wrapper<int>)>’
_M_invoke(_Index_tuple<_Indices...>)
^
作爲一個方面的問題,爲什麼在通用參數按值傳遞時工作:
auto lambda = [](auto v) {};
auto t = std::thread(lambda, vi);
是的,它實際上是一個很好的例子,爲什麼'汽車'很少有意義。它可以是'auto'或'auto &&'。 – SergeyA
@SergeyA你有什麼有意義的例子,汽車和有意義嗎? –