...

/

Automatic Return Type

Automatic Return Type

Now, we'll learn a technique to automatically deduce the return type of a function.

We'll cover the following...

Using auto and decltype together, a function template is able to automatically deduce its return type. Here’s a function with a trailing return type:

Press + to interact
template <typename T1, typename T2>
auto add(T1 fir, T2 sec) -> decltype( fir + sec )
{
return fir + sec;
}

Rules

The syntax above follows a few rules:

  • auto: introduces the syntax for the delayed return type.

  • ...