How to decay rvalues reference type whilst preserving lvalue references in C++14?

218 Views Asked by At

I have the following C++14 lambda

https://godbolt.org/z/aexxTY

#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);
}

First note that boost::optional can contain lvalue references but not rvalue references.

Is it possible with the above generic lambda to handle the rvalue as a copy. What I'd like is something clever like

using T = std::decay_if_rvalue<decltype(t)>::type

Is there something out of the box that does this ?

3

There are 3 best solutions below

1
lubgr On BEST ANSWER

You could use std::is_rvalue_reference together with std::conditional, both from the <type_traits> header.

auto foo = [](auto && t) 
{
    using Orig = decltype(t);
    using T = std::conditional_t<std::is_rvalue_reference<Orig>::value,
            std::decay_t<Orig>, Orig>;
    return boost::optional<T>(std::forward<T>(t));
};

https://godbolt.org/z/WdTdco

0
leslie.yao On

You can write a type trait as

template< class T > struct remove_rvalue_reference       {typedef T type;};
template< class T > struct remove_rvalue_reference <T&&> {typedef T type;};

then

using T = typename remove_rvalue_reference<decltype(t)>::type;

then

auto i = 10;
foo(i);   // T will be int& in the lambda

foo(10);  // T will be int  in the lambda
3
Patrick Roberts On

Expanding on lubgr's answer you can encapsulate the conditional type in an alias compatible with C++14 like this:

#include <type_traits>

template <class T>
using decay_rvalue_reference_t = std::conditional_t<
    std::is_rvalue_reference<T>::value,
    std::decay_t<T>::type,
    T>;

auto foo = [](auto && t) 
{
    using T = decay_rvalue_reference_t<decltype(t)>;
    return boost::optional<T>(std::forward<T>(t));
};

https://godbolt.org/z/YW88Yf