isocpp / isocpp/CppCoreGuidelines

F.41 should suggest returning a struct, not a tuple

Open
#364 90 comments 4 reactions 1 assignee View on GitHub

@gdr-at-ms is already working on this.

Since Nov 12, 2015.

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

Description

I think F.41 ("Prefer to return tuples to multiple out-parameters") should become "Prefer returning aggregate class types to multiple out-parameters" (or something like that, what I mean is "prefer returning a struct").

Structs have names and their fields have names, which helps understanding and reasoning about code. The variables used in std::tie() have names too, but the user needs to declare those names consistently and meaningfully at every point of use; this is error-prone. Also, those variables must be pre-declared, and two-step initialization is not idiomatic in C++ (and particularly ugly if default-construction is not an option).

Tuples might be a better option for certain kinds of generic code, but I don't see them as a candidate for a default guideline on returning multiple values.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.