Le 27/05/2017 à 16:24, Peter Dimov via Boost a écrit :
Niall Douglas wrote:
I would assume an expected
could only return a std::variant from its .error(). I can't think what else it could do. As the foremost authority on the nonexistent expected
, I can tell you what it does: // F shall be in E... template<class F> bool has_error() const; template<class F> F error() const;
// if sizeof...(E) == 1 bool has_error() const; E1 error() const;
It returns the equivalent of variant
not from error(), but from unexpected
unexpected() const; which allows you to
expected
function() { expected e1 = function1(); if( !e1 ) return e1.unexpected(); expected
e2 = function2(); if( !e2 ) return e2.unexpected(); return e1.value() + e2.value(); } Yes this is the reason d'être fro the unexpected function.
I see that the implementation you have done don't allows to return it by reference, and I was wondering if the variant2::subset function (which could for usability purposes be a non-member function) couldn't return by reference. I know that this is something related to changes to the standard. There is currently a thread on C++-Core about the whether it is UB to cast a type to another with a common layout in order to access the common members. It seems that this is valid in C, but this is not anymore valid in C++. If at the end this kind of casts were valid as well in C++, I believe that we could return subset/unexpected by reference without UB. Note that a variant with less alternatives than another one has the "same shared representation". Best, Vicente