我有以下 C++14 lambda
#include <boost/optional.hpp>
int main(){
auto foo = [](auto && t)
{
using T = decltype(t);
return boost::optional<T>(std::forward<T>(t));
};
// This is fine because i is lvalue
auto i = 10;
foo(i);
// This fails because i is rvalue
foo(10);
}
首先注意 boost::optional 可以包含左值引用,但不能包含右值引用。
是否可以使用上述通用 lambda 将右值作为副本处理。我想要的是聪明的东西
using T = std::decay_if_rvalue<decltype(t)>::type
有开箱即用的东西吗?