[英]Perfect forwaring of auto&& in generic lambda
你在这里看到很多反对票。 在给自己的分数之前,请仔细阅读。 这个问题早先被一位 C++ 大师描述并解决了。 读到最后。
下一个通用 lambda 用于保护在线程之间共享资源的任何操作:
auto mutexed = [mtx(std::mutex{})](auto &&fn, auto &&...args) mutable {
std::unique_lock lo(mtx);
return fn(args...);
//return std::forward<decltype(fn)>(fn)(std::forward<decltype(args)>(args)...);
};
decltype(fn)
对完美转发无效。 它适用于值和右值引用。 但不适用于左值引用。
编写decltype((fn))
将适用于左值引用,但不适用于右值。
所以问题。
如何使它完美地转发引用和右值引用?
使线程不破坏标准输出的示例: https : //gcc.godbolt.org/z/KsWM6Pq6x
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
using namespace std::chrono_literals;
using std::cout, std::clog, std::cerr, std::endl;
int main() {
cout << "Hello, World!" << endl;
auto mutexed = [mtx(std::mutex{})](auto &&fn, auto &&...args) mutable {
std::unique_lock lo(mtx);
//return fn(args...);
return std::forward<decltype(fn)>(fn)(std::forward<decltype(args)>(args)...);
};
int counter = 0;
auto t1 = std::thread([&]{
while(1){std::this_thread::sleep_for(10ms);
mutexed( [](int&&cnt){ cout << cnt++ << endl; }, std::move(counter) );} //FAILS HERE
mutexed( [](int&cnt){ cout << cnt++ << endl; }, counter );} //lval ref works fine
});
auto t2 = std::thread([&]{
while(1){
std::this_thread::sleep_for(10ms);
mutexed( []{ cout << "done long_operation_2" << endl;});}
});
t1.join();
t2.join();
}
int main() {
auto lambda20 = []<class F, class...Ts>(F &&fn, Ts &&...args) {
return std::forward<F>(fn)(std::forward<Ts>(args)...);
};
auto lambda14 = [](auto &&fn, auto &&...args) {
return std::forward<
std::conditional_t<
std::is_rvalue_reference_v<decltype(fn)>,
typename std::remove_reference_t<decltype(fn)>,
decltype(fn)>
>(fn)(
std::forward<
std::conditional_t<std::is_rvalue_reference<decltype(args)>::value,
typename std::remove_reference<decltype(args)>::type,
decltype(args)
>>(args)...);
};
int inter = 20;
lambda20([](int x) { cout << "asdf20 x" << endl; }, inter);
lambda20([](int &x) { cout << "asdf20 &x" << endl; }, inter);
lambda20([](int &&x) { cout << "asdf20 &&x" << endl; }, std::move(inter));
lambda14([](int x) { cout << "asdf14 x" << endl; }, inter);
lambda14([](int &x) { cout << "asdf14 &x" << endl; }, inter);
lambda14([](int &&x) { cout << "asdf14 &&x" << endl; }, std::move(inter));
return 0;
}
C++ pre-20 解决方案 8 岁 by Scott Meyers https://scottmeyers.blogspot.com/2013/05/c14-lambdas-and-perfect-forwarding.html
在 C++20 及更高版本中
auto lambda20 = []<class F, class...Ts>(F &&fn, Ts &&...args) {
return std::forward<F>(fn)(std::forward<Ts>(args)...);
};
在 C++pre20 中:C++11,14,17
auto lambda14 = [](auto &&fn, auto &&...args) {
return std::forward<
std::conditional_t<
std::is_rvalue_reference_v<decltype(fn)>,
typename std::remove_reference_t<decltype(fn)>,
decltype(fn)>
>(fn)(
std::forward<
std::conditional_t<std::is_rvalue_reference<decltype(args)>::value,
typename std::remove_reference<decltype(args)>::type,
decltype(args)
>>(args)...);
};
例子
#include <iostream>
using namespace std;
int main() {
auto lambda20 = []<class F, class...Ts>(F &&fn, Ts &&...args) {
return std::forward<F>(fn)(std::forward<Ts>(args)...);
};
auto lambda14 = [](auto &&fn, auto &&...args) {
return std::forward<
std::conditional_t<
std::is_rvalue_reference_v<decltype(fn)>,
typename std::remove_reference_t<decltype(fn)>,
decltype(fn)>
>(fn)(
std::forward<
std::conditional_t<std::is_rvalue_reference<decltype(args)>::value,
typename std::remove_reference<decltype(args)>::type,
decltype(args)
>>(args)...);
};
int inter = 20;
lambda20([](int x) { cout << "asdf20 x" << endl; }, inter);
lambda20([](int &x) { cout << "asdf20 &x" << endl; }, inter);
lambda20([](int &&x) { cout << "asdf20 &&x" << endl; }, std::move(inter));
lambda14([](int x) { cout << "asdf14 x" << endl; }, inter);
lambda14([](int &x) { cout << "asdf14 &x" << endl; }, inter);
lambda14([](int &&x) { cout << "asdf14 &&x" << endl; }, std::move(inter));
return 0;
}
C++ pre-20 解决方案 8 岁 by Scott Meyers https://scottmeyers.blogspot.com/2013/05/c14-lambdas-and-perfect-forwarding.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.