std::variant::valueless_by_exception - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::variant::valueless_by_exception

From cppreference.com
< cpp‎ | utility‎ | variant

constexpr bool valueless_by_exception() const; |  | (since C++17)

Returns `false` if and only if the variant holds a value.

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] Notes

A variant may become valueless in the following situations:

- (guaranteed) an exception is thrown during the move initialization of the contained value from the temporary in copy assignment
- (guaranteed) an exception is thrown during the move initialization of the contained value during move assignment
- (optionally) an exception is thrown when initializing the contained value during a type-changing assignment
- (optionally) an exception is thrown when initializing the contained value during a type-changing emplace

Since variant is never permitted to allocate dynamic memory, previous value cannot be retained in these situations.

This applies even to variants of non-class types:

    struct S {
        operator int() { throw 42; }
    };
    variant<float, int> v{12.f}; // OK
    v.emplace<1>(S()); // v may be valueless

A variant that is valueless by exception is treated as being in an invalid state: index returns variant_npos, get and visit throw bad_variant_access.

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/valueless_by_exception&oldid=91739"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template