Trailing Return Type in C++ 11

In C++11, one of the significant features introduced is the ability to use a trailing return type for function declarations. Before C++11, the return type of a function had to be specified before the function name. However, trailing return types allow you to specify the return type after the parameter list, making it easier to express complex return types, especially in the context of template functions and lambdas.

Trailing return type

The C++11 standard added the trailing return type feature, which lets you declare a function's return type after the parameter list instead of before the function name. Especially in the context of template functions and complex expressions, it is especially helpful in scenarios where the return type depends on the function parameters' types.

Trailing return types are very helpful when working with complex types, especially in the context of template functions. In addition to clearly defining the return type, they allow you to use the auto keyword for function definitions.

Syntax:

It has the following function:

  • auto: Instead of explicitly specifying the return type, the auto keyword indicates that the compiler will deduce the return type.
  • function_name: It is the name of the Function.
  • parameter_list: The list of parameters that the Function takes.
  • -> return_type: The trailing return type comes after the parameter list and uses the arrow (->) syntax to specify the return type.

Example of trailing return type:

Let us take an example to illustrate the trailing return type in C++.

Output:

The Result of x and y is 34

Explanation:

1. Header Inclusion

This line contains the standard input-output stream header (iostream), which offers input and output operations capabilities.

2. Function Definition

It defines the add function, which accepts two int parameters (x and y) and returns an int. The auto keyword uses a trailing return type (-> int). The Function returns the Result of adding the x and y values.

3. Main Function

The main Function is to serve as the program's entry point. Using arguments 11 and 23, it calls the add function, assigns the Result to the variable Result, and then uses std::cout to print the Result.

4. Statement of Return

Upon successful program execution, the operating system receives an integer value of 0 from the main Function.

Template function using a trailing return type

A template function that uses a trailing return type in C++ uses the syntax for trailing return types introduced in C++11. When the return type of a template function depends on the types of the template parameters, the trailing return type is very helpful. It makes the declaration more readable and simple by enabling you to define the return type after the argument list.

Syntax:

It has the following syntax:

  • template <typename T, typename U>: It declares a template with two type parameters, T and U.
  • auto function_name(T parameter1, U parameter2) -> decltype(expression): It is the function declaration with a trailing return type. The auto keyword indicates that the compiler will deduce the return type. The decltype specifier dedicates the return type based on the expression inside it.
  • expression: The expression whose type is used to deduce the return type.

Example of a template function using a trailing return type:

Let us take an example to illustrate the template function using a trailing return type in C++.

Output:

The Result of x and y is 7.5

Explanation:

1. Header Inclusion

This line contains the standard input-output stream header (iostream), which offers input and output operation functionality.

2. Template Function Definition

This function defines a template function named add that takes two template parameters (T and U). The Function returns the Result of adding the parameters x and y. The trailing return type is specified using auto and decltype(x + y), where decltype is used to deduce the expression x + y type.

3. Main Function

The program's starting point is known as the main Function. It assigns the Result to the double variable Result after using the add function with the arguments 3.5 (a double) and 4 (an int). After that, it uses std::cout to print the Result to the console.

4. Output

The result is printed to the console by using std::cout. This example's "The Result of x and y is:" is written after the value stored in the result variable, which is the sum of 3.5 and 4.

5. Statement of Return

Upon successful program execution, the operating system receives an integer value of 0 from the main Function.