Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/rfl/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Error {
Error& operator=(Error&&) = default;

/// Returns the error message, equivalent to .what() in std::exception.
const std::string& what() const { return what_; }
const std::string& what() const & { return what_; }
/// Moves the error message out of Error object and leaves what_ in a moved from state
std::string what() && { return std::move(what_); }
Comment on lines +33 to +35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The introduction of lvalue- and rvalue-qualified overloads for what() is a great improvement for both safety and performance. However, the current implementation has a small gap: it doesn't handle const rvalues.

For example, code like this would fail to compile with the current changes:

const rfl::Error make_const_error() { return rfl::Error("an error"); }
// ...
auto msg = make_const_error().what(); // Fails to compile

This is because make_const_error() returns a const rvalue. The && overload of what() is not const-qualified, and the const & overload requires an lvalue, so neither can be called.

To fix this and support const rvalues, you could add a const && overload. This overload would behave like the const & one, returning a const reference to the string, which is safe. This would make the interface for what() complete with respect to value categories.

  const std::string& what() const & { return what_; }
  /// Moves the error message out of Error object and leaves what_ in a moved from state.
  std::string what() && { return std::move(what_); }
  /// Returns the error message from a const rvalue.
  const std::string& what() const && { return what_; }


private:
/// Documents what went wrong
Expand Down
Loading