Have a simple wrapper struct
template<typename T>
struct named_value {
using type = T;
T value{};
string_view name = "unnamed_value";
constexpr explicit operator T&(){ return value; }
constexpr explicit operator T const&()const{ return value; }
constexpr explicit operator T &&()&&{ return std::move(value); }
template<typename U=T>
constexpr named_value& operator=(U&&v){ value = std::forward<U>(v); return *this; }
friend auto operator<<(std::ostream& os, named_value const& a) ->ostream&
{
return os<<a.name<<":"<<a.value;
}
};
Here I try to use universal reference for operator=
in order to write less overloads (i.e. for ref, rval ref, const ref). Is this right way?