isocpp / isocpp/CppCoreGuidelines

F.45 needs another exception for rvalue overloads

Open
#2,124 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
45.3k
Forks
5.6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the C++ Core Guidelines source entry for rule F.45 and compare its current wording with the proposed exception. Done means the guidance acknowledges rvalue-reference overloads for member accessors such as std::get(std::tuple) and std::optional::value(), with wording consistent with the proposal.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.