-
Notifications
You must be signed in to change notification settings - Fork 1
Description
It has been suggested to let the user decide the Exception that would be throw when the user try to get the expected value but there is none, as third parameter.
While there is no major complexity doing it, as it just needs a third parameter that could default to the appropriated class,
template <class T, class Error, class Exception = bad_expected_access>
struct expected;the authors consider that this is not really needed and that this parameter should not really be part of the type.
The user can use value_or_throw() as in
std::experimental::expected<std::error_code, int> f();
std::experimental::expected<std::error_code, int> e = f();
auto i = e.value_or_throw<std::system_error>();The user can also wrap the proposed class in its own expected class
template <class T, class Error=std::error_code, class Exception=std::system_error>
struct MyExpected {
expected <T,E> v;
MyExpected(expected <T,E> v) : v(v) {}
T value() {
if (e) return v.value();
else throw Exception(v.error());
}
... };and use it as
std::experimental::expected<std::error_code, int> f(); MyExpected<int> e = f();
auto i = e.value(); // std::system_error throw if not validA class like this one could be added to the standard, but this proposal doesn’t request it.
An alternative could be to add a specialization on a error class that gives the storage and the exception to thrown.
template <class Error, class Exception>
struct error_exception {
typedef Error error_type;
typedef Exception exception_type;
};that could be used as
std::experimental::expected<std::error_exception<std::error_code, std::system_error>, T> e = make_unexpected(err);
e.value(); // will throw std::system_error(err);A class like this one could be added to the standard, but this proposal doesn’t request it.
Should it?