Embracing Trailing Return Types and `auto` Return *Safely*
In the old days (and in C), the return type of a function was specified at the beginning of its prototype, after the storage-class specifiers, if any. Modern C++ adds two additional ways to specify a function's return type, adding convenience, uniformity of expression, and, in some cases, clarity. Trailing return types were introduced in C++11 and have several potential advantages over leading return types, especially for function templates and lambda expressions. Automatically deduced return types go a step further, allowing the programmer to eschew specification of the return type entirely and letting the compiler do all of the work. These features can be used to great advantage when the return type of a function is complex or is best expressed directly as the type of an expression. Used unwisely, though, deduced return types can impede readability, create excessive coupling between the interface and the implementation of a function, and create latent bugs in template code. In this talk, we will cover the syntax and restrictions for these two new features, examine their relationship to each other, explore several use cases for each, and -- most importantly -- probe potential pitfalls when attempting to employ these language features.
Pablo Halpern
Pablo Halpern has been programming in C++ since 1989 and has been a member of the C++ Standards Committee since 2007. His major contributions to the standard have been in the areas of parallel and vector programming constructs as well as on improvements to the usability of memory allocators. Pablo is a major contributor to the book, Embracing Modern C++ Safely by John Lakos and Vittorio Romeo (2021) and is the author of The C++ Standard Library from Scratch (2000). His current development focus is in moving the API of the open-source BDE library in the direction of the C++17 PMR allocator model. He lives with his partner in Boston, MA. When not working on books and allocators, he enjoys model rocketry, hiking, skiing, snowboarding, and watching opera. He has recently started learning Pickleball.