isocpp / isocpp/CppCoreGuidelines

When to use trailing return type syntax

Open
#2,066 17 comments 0 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

Inspired by: https://blog.petrzemek.net/2017/01/17/pros-and-cons-of-alternative-function-syntax-in-cpp/

  • When using decltype, as then the return type is free to use parameter names too. If this is thought to be too strict, then use trailing return when the decltype expression would otherwise have to construct a type instance (e.g. using std::declval) when there is also a function parameter of that type which could be used instead (which would be easier to read).
  • When defining a method outside of the class declaration, and the return type is a type alias defined inside the same class as the method. This avoids repeating class_name:: for the return type.
    • Similarly when decltype is used for a class method return type with an expression using class_name::some_member.
  • When the function returns a function pointer. This is much easier to read.

More generally, it would be good to recommend trailing return whenever the return type has more than one alpha-numeric token, so the function name is easy to read quickly. (This would obviate the need for the first two rules above).

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 by reading the linked article and the full discussion on this issue, then compare the proposed cases with the existing C++ Core Guidelines recommendations. Done means reaching a settled recommendation for trailing return type syntax and documenting it in the relevant guideline, although no target file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.