In C++11, a notable addition is the option to employ a trailing return type in function declarations. In earlier versions of C++, the return type needed to be declared before the function name. Nonetheless, trailing return types enable you to define the return type after the parameter list, simplifying the expression of intricate return types, particularly in template functions and lambdas.
Trailing return type
The C++11 standard introduced the trailing return type functionality, allowing the declaration of a function's return type after the parameter list rather than before the function name. This feature is particularly beneficial when dealing with template functions and intricate expressions, proving to be advantageous in situations where the return type is contingent on the types of the function parameters.
Trailing return types are extremely beneficial when dealing with intricate types, particularly within the realm of template functions. Apart from explicitly specifying the return type, they enable the utilization of the auto keyword in defining functions.
Syntax:
It has the following function:
auto function_name(parameter_list) -> return_type
{
// function body
}
- 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's consider an example to demonstrate the trailing return type feature in the C++ programming language.
#include <iostream>
//Function with a trailing return type
auto add(int x, int y) -> int
{
return x + y;
}
int main()
{
int result = add(11, 23);
std::cout << "The Result of x and y is " << result << std::endl;
return 0;
}
Output:
The Result of x and y is 34
Explanation:
- Header Inclusion
This line features the standard input-output stream header (iostream) that provides functionalities for input and output operations.
- Defining a Function
It declares the add function, taking two integer parameters (x and y) and returning an integer. The auto keyword is employed with a trailing return type (-> int). This function yields the sum of the x and y values.
- Entry Point
The primary purpose of the main Function is to act as the starting point of the program. By passing arguments 11 and 23, it invokes the add function, stores the outcome in the variable Result, and then utilizes std::cout to display the Result.
- Description of Return
After the program runs successfully, the main Function returns an integer value of 0 to the operating system.
Template function using a trailing return type
A template function that employs a trailing return type in C++ utilizes the syntax for trailing return types that was introduced in C++11. When the return type of a template function is reliant on the types of the template parameters, the trailing return type proves to be extremely beneficial. It enhances the readability and simplicity of the declaration by allowing you to specify the return type after the argument list.
Syntax:
It has the following syntax:
template <typename T, typename U>
auto function_name(T parameter1, U parameter2) -> decltype(expression)
{
// function body
}
- 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's consider a scenario to demonstrate the template function utilizing a trailing return type in the C++ programming language.
#include <iostream>
// Template function with a trailing return type
template <typename T, typename U>
auto add(T x, U y) -> decltype(x + y)
{
return x + y;
}
int main()
{
double result = add(3.5, 4);
std::cout << "The Result of x and y is " << result << std::endl;
return 0;
}
Output:
The Result of x and y is 7.5
Explanation:
- Header Inclusion
This line includes the iostream header, which provides functionality for input and output operations.
- Definition of a Template Function
This function creates a template function called "add" with two template parameters (T and U). It computes the sum of the input parameters x and y, utilizing the trailing return type specified by auto and decltype(x + y) to deduce the type of the expression x + y.
- Entry Point Function
The initial point of the program is referred to as the main Function. It sets the Outcome to the double variable Outcome following the utilization of the add function with the parameters 3.5 (a double) and 4 (an int). Subsequently, it employs std::cout to display the Outcome on the console.
- Output
The outcome is displayed on the console using std::cout. In this instance, "The Sum of x and y is:" is outputted following the computed value stored in the result variable, representing the total of 3.5 and 4.
- Return Statement Explanation
After a program runs without errors, the main function returns an integer value of 0 to the operating system.