Skip to content

[expected.object.obs] Inconsistency between value_or() and error_or() in std::expected<T, E> #7913

Open
@onihusube

Description

@onihusube

[expected.object.obs]/21

The return value of value_or() is specified as follows:

Returns: has_value() ? **this : static_cast<T>(std​::​forward<U>(v)).

Meanwhile, the return value of error_or() is specified as follows:

Returns: std​::​forward<G>(e) if has_value() is true, error() otherwise.

Since these functions appear to be dual in nature, it would be preferable to maintain consistent notation. Therefore, should error_or() be modified in this way?

Returns: has_value() ? std​::​forward<G>(e) : error().

There is another problem at this time.

value_or() casts its argument v using static_cast<T>(std::forward<U>(v)). Shouldn't error_or() similarly perform a static_cast<E>?

When the constructor used for conversion is marked as explicit, value_or() will successfully perform the conversion, while error_or() will result in a compilation error.

Here I referred to const & overload, but && overload has the same problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions