C++11 returns an example of the use of post type syntax

  • 2020-05-30 20:49:09
  • OfStack

The addition of auto to the C++11 standard not only automatically inferred the variable type, but also combined with decltype to represent the return value of a function. These new features allow us to write cleaner, more modern code.

In generic programming, you may need to perform an operation on a parameter to get the type of the return value.

Let's take a look at the following example:


#include<iostream>
using namespace std;


template <typename R,typename T, typename U>
R add(T t,U u)
{
  return t+u;
}

int main()
{
  int a=1;
  float b=2.0;
  auto c = add<decltype(a+b)>(a,b);
}

We don't care what type a+b is, because we just need to get the return value type directly from decltype(a+b). But it's not convenient to use 10 points like above, because the outside doesn't really know how the parameters should be evaluated, only the add function knows how the return value should be derived.

Can we get the return value directly from the function definition via decltype? Like this:


template <typename T, typename U>
decltype(t+u) add(T t,U u)      // Compile error, t,u undefined 
{
  return t+u;
}

When run, the compiler will tell us that t and u in decltype(t+u) have not been declared in this scope.

Since t and u are in the parameter list, and the return value of C++ is a prepositional syntax, the parameter variable does not exist when the return value is defined.

For this example, write it as follows:


template <typename T, typename U>
decltype(T()+U()) add(T t,U u)    
{
  return t+u;
}

Considering that T and U may be classes with no arguments constructors, the correct way to write them is as follows:


template <typename T, typename U>
decltype((*(T*)0)+(*(U*)0)) add(T t,U u)    
{
  return t+u;
}

Although the derivation of the return value is accomplished successfully with decltype, it is too obscure to write decltype in a way that greatly increases the difficulty of deriving the return value type and reduces the readability of the code.

Therefore, in C++11, the return type postscript syntax was added to complete the derivation of return value types by combining decltype and auto.

The return type postscript syntax is used in combination with auto and decltype. The add function above, using the new syntax, can be written as:


template <typename T, typename U>
auto add(T t,U u) ->decltype(t+u)    
{
  return t+u;
}

To further illustrate this syntax, take a look at another example:


#include<iostream>
using namespace std;


int& foo(int& i);
float foo(float& f);

template <typename T>
auto func(T& val) -> decltype(foo(val))
{
  return foo(val);
}

In this example, it is easy to derive the possible return value types for foo(val) using decltype in conjunction with the return value postscript syntax and apply them to func.

The posttype syntax for return value types is to solve the problem that the return value types of functions depend on parameters, which makes it difficult to determine the return value types. With this syntax in place, the derivation of return value types can be described in a clear way.


Related articles: