-1

I recently discovered that in C++ , we can initialize the integer , or for that matter other primitive data type in C++ using different ways.

The first one that most would be familiar is :

int i = 50;       //initialised to 50
char c = 'a';     //initalised to 'a'
//And in similar way for all other data types

The one that I got to know recently is done as :

int i(50);       //initialised to 50
char c('a');     //initalised to 'a'

Experimenting some more, I also found that the same thing can also be done in yet another way in C++ ! as follows :

int i{50};       //initialised to 50 just as before
char c{'a'};     //initalised to 'a'...same again

These are applicable to all other primitive data-types in C++ as well. Now I am can't help but wonder if the above different types of data initialisation are different in any possible way at all?

  1. Is one more efficient than other? Does using one over other have any benefits at all?
  2. Is there any scenario where one is preferable over other?
  3. If not, why all these different kinds of initialisation are provided? If yes, can you give some scenario where each has its place of use?
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • 2
    related/dupe: https://stackoverflow.com/questions/1051379/is-there-a-difference-between-copy-initialization-and-direct-initialization – NathanOliver May 26 '20 at 19:37
  • @NathanOliver I have already checked few of answers on that post. Thanks for mentioning it anyway. But none of them explicitly answer as to why there exist all these different ways of intialisation and if there's any which is more efficient over other and if there are any scenario where one is used/preferred over other. – Abhishek Bhagate May 26 '20 at 19:40
  • Close, but that question doesn't cover uniform initialization – Mooing Duck May 26 '20 at 19:41
  • 2
    There are a bunch of different ways to initialize stuff in C++ and the rules behind them can get complicated. If you really want the whole picture, see https://en.cppreference.com/w/cpp/language/initialization. – eesiraed May 26 '20 at 19:41
  • @Abhishek: Note: `std::string s = "hi";` and `std::string s("hi")` are valid and the same, but `std::string s{"hi"}` does not compile. – Mooing Duck May 26 '20 at 19:42
  • 1
    Related: [https://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives](https://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives) – drescherjm May 26 '20 at 19:42
  • Thanks for adding that @MooingDuck :) – Abhishek Bhagate May 26 '20 at 19:45
  • 1
    This is some entertaining viewing on the topic: [CppCon 2018: Nicolai Josuttis “The Nightmare of Initialization in C++”](https://www.youtube.com/watch?v=7DTlWPgX6zs) – user4581301 May 26 '20 at 19:52
  • @user4581301 Thanks for the link. That mentions a lot more other intialization types. I will be sure to watch that ! – Abhishek Bhagate May 26 '20 at 19:58
  • @BessieTheCow Thanks for the link. I took a look at that but the source doesn't comment or mention anything about the use cases or why one exists despite other ways being already present and supposedly doing exact same thing. – Abhishek Bhagate May 26 '20 at 20:02

1 Answers1

4

Is one more efficient than other?

No.

Is there any scenario where one is preferable over other?

Curly braces are often preferable, when they are an option, because they do not allow narrowing implicit conversions, which are often unintentional.

Note that curly braces and parentheses affect the resolution of the constructor in case of class types. In that case, prefer the ones that resolve to the constructor that you intend to use.

why all these different kinds of initialisation are provided?

  1. Backward compatibility
  2. Generic programming (templates)
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I suppose curly braces initialization will internally call the constructor to initialize the variable(though I could be wrong. Correct me if I am.). If that's the case, wouldn't it be beneficial to just call the constructor explicitly and reduce the overhead? – Abhishek Bhagate May 26 '20 at 19:57
  • @Abhishek There is no cost. Either one directly calls the best-matching constructor. Note eerorika's second point: that sometimes this match is different: `std::vector a{10,2};` and `std::vector b(10,2);` have wildly different behaviour. – user4581301 May 26 '20 at 20:07
  • 1
    @Abhishek The syntax has no effect on whether the constructor is called. – eerorika May 26 '20 at 20:12