Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F.45 needs another exception for rvalue overloads #2124

Open
Eisenwave opened this issue Aug 16, 2023 · 0 comments
Open

F.45 needs another exception for rvalue overloads #2124

Eisenwave opened this issue Aug 16, 2023 · 0 comments

Comments

@Eisenwave
Copy link
Contributor

Eisenwave commented Aug 16, 2023

F.45 is titled "Don’t return a T&&" and states:

We don’t know of any other good examples [besides std::move] of returning &&.

This is ignorant of functions such as:

The recommendations of CppCoreGuidelines shouldn't contradict common practice in modern C++ standard library features. One could argue that even the standard library uses are illegitimate and unsafe, and there is truth to that. However, the user would incur unjust cost for code such as:

// if .value() returned a value instead of an rvalue reference,
// this would unnecessarily create a temporary object
vec.emplace(get_optional().value());

The zero-overhead principle, a core design principle of the language from the very start arguably trumps the slight safety benefit of returning a value.

Proposed Changes

-We don’t know of any other good examples of returning &&.

+##### Exception
+Member accessors such as `std::get(std::tuple)`,
+or `std::optional::value()` have overloads for rvalue references,
+and those overloads return rvalue references.
+This is fine because it simply imitates what happens when accessing a member of an rvalue struct,
+or a member of an rvalue array.
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

No branches or pull requests

1 participant