Jonathan Boccara's blog

Trailing Return Types

Published April 16, 2021 - 0 Comments

This article is NWH, standing for Not Written Here. The concept of NWH is inspired from the NIH (Not Invented Here) syndrome which consists in refraining from using existing code from outside the company and reinventing the wheel every time. Just like it is good practice to look out for solutions developed elsewhere, we’re going to look at an article written elsewhere. Or said differently, an article that is NWH.

The NWH contents presented on Fluent C++ relate to the theme of writing better code, just like the homemade posts. If you like the usual contents on Fluent C++, you’ll also like to discover the NWH.

C++11 introduced trailing return types, that is a new syntax for function prototypes. Instead of writing the return type on the left hand side, like this:

ReturnType function(Parameter1 const& parameter1, Parameter2 parameter2)
{
    // ...

C++11 allows to put the trailing return type at the end, after an arrow. The syntax also require to write auto on the left-hand side of the prototype:

auto function(Parameter1 const& parameter1, Parameter2 parameter2) -> ReturnType
{
    // ...

Is it better? Is it worse? Which code is the most expressive? Are there exceptions?

It turns out there are many aspects to consider in this new syntax, many pros and cons, to decide whether to use it or not in your code.

This is the theme of the NWH of today, on Petr Zemek’s blog: Pros and Cons of Alternative Function Syntax in C++.

Since this alternative function syntax is different from what most C++ developers have been taught to write, it might seem a bit alien. Very much like the “auto to stick” syntax.

I know first hand that auto to stick can become quite natural when you start using it. Can trailing return type become natural too? Would you adopt them?

You will also like

Don't want to miss out ? Follow:   twitterlinkedinrss
Share this post!Facebooktwitterlinkedin