这是问题的第 2 部分有没有办法将左值和右值列表分别转换为具有引用类型和完整类型的元组?
我想根据参数列表的左值/右值类型创建一个元组(或对)的元组。这是我到目前为止:
#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() )
);
}
这适用于右值,但是当我将它用于左值时,它会说 reference_wrapper<T> requires T to be an object type or a function type.
我错过了什么?
我错过了什么?
template <typename T>
detail::VarVal<std::reference_wrapper<T>> vv(LPCWSTR var, T& val)
{
return{ var, val };
}
std::reference_wrapper<T>
, 不是 std::reference_wrapper<T&>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句