Skip to content

Mark std::expected as [[nodiscard]] #130656

@2LoS

Description

@2LoS

It is problematic to discard the result of a function that uses std::expected for error handling. It is the equivalent of enclosing an exception-throwing function call in a try-catch block, where any caught exception is just ignored.

try {
  func();
}
catch(...) {}

The language allows it to happen without any ceremony. It should, of course, be possible to write the ceremony explicitly.

(void)func();

The way to make this happen is marking std::expected as nodiscard. For example, Microsoft STL and martinmoene/expected-lite have already done it. Also libc++ has fixed its tests to support the case in which std::expected be marked as nodiscard, even if its own library has not marked it yet.

Metadata

Metadata

Assignees

Labels

libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions