C++11 introduced the auto keyword as a placeholder for types. It could be used for declaring variables but also for the return type of a function, given than you use a new syntax with a trailing type at the end of the function.

auto foo() -> int { return 42; } 1 2 3 4 auto foo ( ) -> int { return 42 ; }

or

template <typename T> auto foo(T value) -> decltype(value.bar()) { return value.bar(); } 1 2 3 4 5 template < typename T > auto foo ( T value ) -> decltype ( value . bar ( ) ) { return value . bar ( ) ; }

This is a bit cumbersome and too explicit so C++14 takes the next step and makes the trailing return type unnecessary. Under C++14 the above examples can be simply written as:

auto foo() { return 42; } 1 2 3 4 auto foo ( ) { return 42 ; }

and

template <typename T> auto foo(T value) { return value.bar(); } 1 2 3 4 5 template < typename T > auto foo ( T value ) { return value . bar ( ) ; }

There are several things to keep in mind:

if a function has more than one return statement they all must deduce the same type

auto foo() { if(condition) return 1; return 0; } 1 2 3 4 5 6 7 auto foo ( ) { if ( condition ) return 1 ; return 0 ; }

a recursive function can have an auto return type only if it has a non-recursive return statement before the recursive call.

auto fibonacci(int n) { if(n<=2) return 1; else return fibonacci(n-1) + fibonacci(n-2); } 1 2 3 4 5 auto fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return fibonacci ( n - 1 ) + fibonacci ( n - 2 ) ; }

a function with an auto return type can be forward declared, but cannot be used until it has been defined

In my personal opinion auto return type should be used with caution. It can make the code harder to read and maintain (you don’t know the type just by looking at the signature), harder to document (automatic documenting tools use the signature to produce documentation), and can lead to situations when the developer expects one return type but the compiler generates another.

Additional readings:

Share this: Twitter

LinkedIn

StumbleUpon

Facebook

Reddit

More

Google

Email



Print

