C++14 auto :new features


The features discussed below are some of the new features added in C++14 auto.

i)To specify the return type of a function

One of the new feature added in the C++14 auto is auto can now be used to specify the return type of the function.Instead of giving the return type explicitly in a function definition we can just use auto.

auto increment_twice( int i) ///wok fine
{
return ++(++i)
}

int main( )
{
cout<< increment_twice(3) ;

return 0 ;
}


ii)Multiple return statement must return the same type

Although auto can specify the return type ,however if the function contain a multiple return statements then all of it must return the same type.Consider the function shown below which return a char and int type value.

auto func( int val )
{
if( val>0 )
 return ‘C’ ; //error returns char type
else
 return ++val ; ///returns int type
}

int main( )
{
cout<< func( ) ;

return 0 ;
}

In running the program you will get an error.To prevent from getting the error we must make the first return statement return an int type,we can achieve this by doing a simple cast.

auto func( int val )
{
if( val>0 )
 return (int)’C’ ; //work fine,returns int type
else
 return ++val ; ///returns int type
}





iii)function using auto to specify the return type must be defined before it is call

When the return type of a function is specify by auto the function must be always defined before it is call.For a normal function the format of defining the function shown below is valid.

int foo( ); //function declaration

int main( )
{
cout<< func() ; //funciton foo() is used here

return 0;
}

///function foo() is defined here
int foo( )
{
return 1;
}

The function foo( ) is defined after it is used in main() function.But if the foo( ) return type is specify by auto then the above format of defining the function foo() is an error.So,the format shown below will give you an error.

auto foo( ); //function declaration

int main( )
{
cout<< func() ;///error function is call before it is defined

return 0;
}

///function foo( ) is defined here
auto foo( )
{
return 1;
}

So make sure that you defined foo() before main() function or any function that calls it.Another example is shown below where the same error is produce.

auto foo( ) ;

auto func(int val )
{
return foo()*val ; ///error foo() is defined after func()
}

auto foo( ) { return 2; }

The correct format of using foo( ) is.

auto foo( ) { return 2; } ///foo() defined before func( )

auto func(int val )
{
return foo( )*val ; ///work fine
}


 



iv)Recursive function

If a recursive function return type is specify by an auto then inside the function definition there must be at least one return statement before the function is called again.Consider the recursive function given below.

auto factorial( int val )
{
static int product = val ;

if (val != 1)
{
 –val;
 product = product*val;
 factorial(val); //error
}
else
{
 return product ;
}

}

The above function will give you an error.It must be modified to have a return statement before the function is called again.One of the working code is shown below.

auto factorial(int val )
{
static int product = val ;
if (val == 1)
{
  return product ; ///return statement before the factorial() is called again
}
else
{
 –val ;
 product = product*val ;
 factorial(val); //work fine
}
}

Now the function will work.

The new features added in C++14 for auto does enriched the properties of auto making it more versatile, however there are some restrictions.But if we put these restrictions aside there is no doubt auto is now more valuable than it’s predecessor C++11 auto.


Related Link

->C++11 auto