C++11 initializer_list everything you need to know about

The C++11 initializer_list is another feature beside ellipsis(or variable argument list) which allow function to accept any number of arguments.Here we will discuss everything about initializer_list and also also try to draw a conclusion among ellipsis or initializer_list which should be preferred over which and when.

Link :C++ ellipsis

Ellipsis was inherited from C so it represents the C way of allowing function to accept multiple arguments without it being checked by the compiler.The C++ standard has introduced a new feature like the ellipses to allow passing of unknown number of arguments to the function.

In this case the type of all the arguments must be same.This feature can be implemented using the initializer_list parameter and this parameter is included in the library header <initializer_list> .The program below implements the feature.

void func( initializer_list<int> li )
{
 for ( auto l = li.begin() ; l != li.end() ; l++ )
 {
  cout << *l << endl ;
 }
}

int main( )
{
func( { 23 , 2, 90, 0, 100, 2345 } ) ;

cin.get( ) ;
return 0 ;
} 

Some notes,

i) li.begin() points to the beginning of the initializer_list ‘li’.Since we are going to access all the arguments holds bu ‘li’ we can start accessing it only after we make ‘l’ point to the beginning of ‘lI’.
 
ii)li.end() points to the end of the ‘li’ list.The ends determines when to stop accessing the ‘li’.
 
iii)As shown in the program all the arguments must be written inside the ‘{ }'(braces) and they must be of the same type.


Comparison of initializer_list with ellipsis

Initializer_list allow passing of single type but varying number of arguments to a function with a single initializer_list parameter.This means if we want to pass another argument of different type then we have to declare another initializer_list parameter for that second argument type.

typedef initializer_list<string> ilstring;
typedef initializer_list<char> ilchar;

string append( ilstring st , ilchar c )
{
 string str=”” ;

 for (auto ls = st.begin( ) ; ls != st.end( ) ; ls++ )
 {
  str += *ls ;
 }

 for(auto cc = c.begin(); cc!=c.end() ; cc++ )
 {
  char ch = *cc ;
  str += ch;
 }

return str ;
}

int main()
{
cout << append({ “hello”} , { ‘d’ }) << endl;

cin.get( );
return 0 ;
} 

For passing string argument(“hello”) a string type initializer_list parameter is declared and for char(‘d’) argument a char type initializer_list parameter is declared.

Looking at this scenario we can obviously say that initializer_list behave like the normal parameters which require a distinct declaration of each parameter type for each different argument type passed.

If we look at ellipsis declaring different parameters for each argument type is superfluous even if the arguments passed are all distinct.This make declaration of parameters with ellipsis (actually not declared) clear cut and simpler.

string Append(int i , … )
{
 va_list start ;

 va_start(start, i);

 string s = va_arg(start, char*) ;
 char c = va_arg(start, char);

 s += c;

 va_end(start);
 return s ;
}

int main()
{
cout<< Append(0 , “Doom ” , ‘d’ ) ;

return 0; 
}

By looking at the program we can say using ellipsis in a function is simpler.But hold on! let’s not come to conclusion yet.

If we want to access the values passed to the ellipsis then we must use some macros (va_start(),va_arg ,…).However,to access the value with va_arg(),each type of the argument must be known and this is quite problematic because the type cannot be known in any way,except when the client or user pass only one type of argument.However, if a client pass only one type of argument we are better of using initializer_list as parameter.

With initializer_list parameter the definite number of arguments can be known,whereas with ellipsis it is impossible to know the number of arguments and so while accessing the value we might read a memory beyond the actual argument storage and this is dangerous.One solution to this problem was to pass the number of arguments as the first argument whenever ellipsis base function is called.

But,if initializer_list is used passing the the first argument becomes redundant.Moreover,accessing the value in initializer_list is a whole lot simpler and easier.The question is,if initializer_list can process indeterminate number of arguments with ease compare to ellipsis why not used it instead of ellipsis? off course we must!.

The bottom line is if only one type but indeterminate number of arguments is required initializer_list is the
better choice but if random type is required ellipsis can be used but you have to figure out the type at each position of the argument list.



Leave a Reply

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