liveBook · Manning

Trailing return type

As explained in Learn C++ by Example

A trailing return type is C++ syntax that places a function's return type after its parameter list using an arrow.

A trailing return type places a function’s return type after its parameter list: auto main() -> int. C++11 introduced the syntax, which is optional for ordinary functions but helps express some template return types. section 1.4.2

A minimal program

Use the syntax in a small Hello World program: auto main() -> int. The traditional int main() remains valid, and main returns 0 by default. section 1.4.2

Listing 1.1 Hello, World
#include <iostream>                    #1
 
auto main() -> int {                   #2
    std::cout << "Hello, world!\n";    #3
}

Inspect the complete Hello World listing to see auto, the arrow, and the return type working together. section 1.4.2

Template return types

A template can place decltype(lhs + rhs) after its parameters, allowing simple_plus to return the type produced by adding two potentially different operands. section 1.4.2

How Learn C++ by Example covers it

How deep it goes
Defines the syntax, contrasts it with traditional notation, and demonstrates decltype without pursuing deeper language internals.
Examples
Uses Hello World, main, and a simple_plus template accepting operands of different types.
What it assumes
Assumes recognition of include, scope resolution, stream insertion, and main while introducing auto and the arrow syntax.
What you can do after
Apply it via C++ function syntax and template definitions requiring deduced return types.
How it connects
Connects auto with function templates and decltype-based return-type deduction.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.