Skip to content

Instantly share code, notes, and snippets.

@ipapadop
Last active August 18, 2020 18:20
Show Gist options
  • Save ipapadop/184d4e4c9ce7e24198c1183578471d26 to your computer and use it in GitHub Desktop.
Save ipapadop/184d4e4c9ce7e24198c1183578471d26 to your computer and use it in GitHub Desktop.
Proper forwarding
// see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0644r1.html
// C++14
template <class X, class Y>
decltype(auto) foo(X&& x, Y&& y) {
return std::forward<X>(x)(std::forward<Y>(y));
}
// C++14 (?)
#define FWD(x) std::forward<decltype(x)>(x)
template <class X, class Y>
decltype(auto) foo(X&& x, Y&& y) {
return FWD(x)(FWD(y));
}
auto bar = [](auto&& x, auto&& y) {
return FWD(x)(FWD(y));
};
// C++17
auto bar = [](auto&& x, auto&& y) {
return std::forward<decltype(x)>(x)(std::forward<decltype(y)>(y));
};
// C++20
// https://www.bfilipek.com/2020/08/lambda-syntax.html
auto bar = []<class X, class Y>(X&& x, Y&& y) {
return std::forward<X>(x)(std::forward<Y>(y));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment