C++14 auto :new features

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

Link : C++14 template variable and aggregate member initialization


i)To specify the return type of a function

C++14 allows auto to specify the return type of the function. Instead of writing the return type explicitly in a function definition we can simply use the auto keyword.

auto increment_twice( int i) //works 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.

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

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

return 0 ;
}

In running the program you will get an error because the first return statement returns a char but the second return statement returns an int type. So there is a type conflict here.

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
}

If you make the second return stament to return a char type, the program would have just work fine too. The trick is to make the two return staments return the same type.

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


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

When the return type of a function is specify by the auto keyword the function must be always defined before it is called. For a normal function, the format of defining the function which is 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 called in the main() function. But, if foo( ) return type is specify by the auto keyword then the above format of defining the function foo() is an error. Consider the example below.

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 produced.

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 is 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 enrich the properties of auto by making it more versatile, however, there are some restrictions. But if we put these restrictions aside there is no doubt that auto is now more valuable than it’s predecessor C++11 auto.




Leave a Reply

Your email address will not be published. Required fields are marked *