Possibility to avoid {} in initializer_list instances.

Apr 15, 2013 at 8:04pm
Is there any possible way to avoid the curly brackets in the following call?
1
2
3
4
5
  void funct(initializer_list<int> args){/*...*/}
  //...
  int main(){
     funct( {1,2,3} );   //<--This line
  }


I tried searching it up but got nowhere fast.



On a random note, I like the new "New Topic" box with the "Write your question here" and the automatic code tags. :3
Apr 15, 2013 at 8:07pm
AFAIK there isn't. By definition initializer_lists are wrapped in braces.

Ah! Just noticing they put that in there as well. That's going to help a lot :P
Apr 15, 2013 at 8:08pm
Why do you want to avoid using the curly braces? The items would then be interpreted as individual parameters, and you could just use a vararg function to handle that situation, but your current code is much cleaner.
Apr 15, 2013 at 8:11pm
Oh, well thank you anyway. I am worried about my team members using my function without the brackets because they won't expect an initializer_list... Even I'm worried about forgetting that I need the brackets and start using the function blindly.
Apr 15, 2013 at 8:12pm
A braced-init-list is required.

Except that we can pass an empty initializer list with
funct( std::initializer_list<int>() ) ;
Apr 15, 2013 at 8:58pm
Daleth wrote:
I'm worried about forgetting that I need the brackets
Fear not. If you forget the brackets the compiler will tell you very clearly that you have made that mistake, then you can correct it swiftly.
Topic archived. No new replies allowed.