Skip to content

When to use trailing return type syntax #2066

@ntrel

Description

@ntrel

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions