vendredi 27 octobre 2017

Is there a way to convert a list of lvalues and rvalues to a tuple of tuples with reference types and full types respectively?

This is part 2 of question Is there a way to convert a list of lvalues and rvalues to a tuple with reference types and full types respectively?

I would like to create a tuple of tuples (or pairs) based on the lvalue/rvalue type of the parameter list. This is what I have so far:

#include <tuple>
using LPCWSTR = wchar_t const*;

namespace detail
{
    template <typename T>
    struct VarVal : std::pair<LPCWSTR, T>
    {
        // Importing the base constructors so I don't have to redefine them
        using std::pair<LPCWSTR, T>::pair;
        VarVal(VarVal const&) = delete; // copy could be made valid, but I don't want it copied around.
        VarVal(VarVal&&) = default; // would rather that no copying/moving be done, but not sure how
    };
}

// lvalue
template <typename T>
detail::VarVal<std::reference_wrapper<T&>> vv(LPCWSTR var, T& val)
{
    return{ var, val };
}

// rvalue
template <typename T>
detail::VarVal<T const> vv(LPCWSTR var, T&& val)
{
    return{ var, val };
}

struct SomeType
{
    int x;
    auto GetLeft()   const { return  1; }
    auto& GetRight()  const { return  x; }
};

auto varvals(SomeType const& object)
{
    return make_tuple(
        vv( L"left",  object.GetLeft() ),
        vv( L"right", object.GetRight() )
    );
}

Demo

This works for rvalues, but when I use it for an lvalue, it barfs saying reference_wrapper<T> requires T to be an object type or a function type.

What am I missing?

Aucun commentaire:

Enregistrer un commentaire