There are two distinct styles for declaring functions, each employing a different approach to specifying the return type. The first adheres to the traditional syntax:
bool getMode() const;
On the other hand, the second leverages the auto keyword and the arrow syntax for the return type:
auto getMode() const -> bool;
My primary question is whether there are any substantial reasons to prefer one style over the other. I understand that readability is often subjective, but are there any specific scenarios, best practices, or performance considerations that could guide the choice between these two styles?
There is one semantic difference between the two, and it occurs when implementing a class method outside of the class. Suppose we have a class that defines a
typedefand an instance method.Now we go to our
.cppfile to implement this method. If we use trailing return type syntax, then everything just works.However, if we put the type in front, then we have to prefix it with
Example::This is simply because of the compiler's parse order. The compiler only "figures out" that we're inside of the class
Exampleonce it seesExample::foobar. If the return type is trailing, then it occurs after the qualified name of the method, so we know the scope. If the return type occurs before we see that name, then the return type is evaluated in the surrounding scope and has to be fully qualified.This gets even messier with template classes. Compare