Skip to content

Conversation

BestITUserEUW
Copy link
Contributor

…load to allow moving Error::what_ to caller if Error is an rvalue. Adresses #504

…load to allow moving Error::what_ to caller if Error is an rvalue
Copy link

Summary of Changes

Hello @BestITUserEUW, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Error class by refining how its internal error message is accessed. It introduces explicit reference qualifiers to the what() method, providing distinct behaviors for lvalue and rvalue Error objects. Specifically, an rvalue-qualified overload enables the efficient moving of the error message, which can lead to performance improvements by eliminating unnecessary string copies when dealing with temporary Error objects. This change directly addresses issue #504.

Highlights

  • Explicit Reference Qualifiers: The what() method in the Error class now explicitly uses an lvalue-reference qualifier (const &) to indicate it operates on lvalue Error objects.
  • Rvalue Overload for what(): An additional overload for what() has been introduced with an rvalue-reference qualifier (&&), allowing the internal error message (what_) to be moved out of an rvalue Error object, improving efficiency by avoiding copies.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@BestITUserEUW
Copy link
Contributor Author

@liuzicheng1987 Do you want me to setup tests for Error::what() ?

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces ref-qualified overloads for Error::what(), which is a great improvement for allowing moves from rvalue Error objects, enhancing both performance and safety. My review includes a suggestion to add a const && overload to fully support all value categories and prevent a potential compilation failure with const rvalue Error objects.

Comment on lines +33 to +35
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_); }

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_; }

@liuzicheng1987 liuzicheng1987 merged commit 075488a into getml:main Sep 29, 2025
146 checks passed
@liuzicheng1987
Copy link
Contributor

@BestITUserEUW , no, I think that would be overkill. Thanks for your contribution!

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

Successfully merging this pull request may close these issues.

2 participants