Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rarer valueless_by_exception #25

Closed
mpark opened this issue Apr 21, 2017 · 3 comments
Closed

Rarer valueless_by_exception #25

mpark opened this issue Apr 21, 2017 · 3 comments

Comments

@mpark
Copy link
Owner

mpark commented Apr 21, 2017

Implement section III of P0308 as a non-standard version (outside of cpp17 namespace).

Types such as variant<int, double> should not ever get into a valueless_by_exception state.
Something like:

template <std::size_t I, typename... Args>
inline auto &emplace(Args &&... args) {
  using T = variants::lib::type_pack_element_t<I, Ts...>;
  if constexpr (std::is_nothrow_constructible_v<T, Args...> ||
                !std::is_nothrow_move_constructible_v<T>) {
    this->destroy();
    auto &result = this->construct_alt(access::base::get_alt<I>(*this),
                                       std::forward<Args>(args)...);
    this->index_ = I;
    return result;
  } else {
    T temp(std::forward<Args>(args)...);
    this->destroy();
    auto &result = this->construct_alt(access::base::get_alt<I>(*this), std::move(temp));
    this->index_ = I;
    return result;
  }
}
@viboes
Copy link

viboes commented Apr 21, 2017

+1
If I had a variant that can ensure the non-empty guaranties I would try to implement std::expected wrapping a stdxx::variant.

Note that std::expected could ensure the non-empty guaranties as the type E must be nothrow as used to report errors.

@mpark
Copy link
Owner Author

mpark commented Apr 21, 2017

It's not clear to me how this ticket would help with the implementation of expected. Even if E is required to be nothrow move, T doesn't have that requirement right? So it seems to me like it wouldn't help in the general case? or does expected have optional-like behavior where it falls back to the default-constructed error state or something?

@viboes
Copy link

viboes commented Apr 21, 2017

Hrr, maybe you are right and I was thinking on P0110R0.
No, neither std::expected nor std::optional fallbacks, at least I don't remember of that.

@mpark mpark closed this as completed Nov 13, 2017
@mpark mpark changed the title Rarer valueless_by_exception. Rarer valueless_by_exception Dec 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants