C++ Initialization types - practical use

Viewed 187

According to cppreference, there are the following initialization types:

  • Value initialization, e.g. std::string s{};
  • Direct initialization, e.g. std::string s("hello");
  • Copy initialization, e.g. std::string s = "hello";
  • List initialization, e.g. std::string s{'a', 'b', 'c'};
  • Aggregate initialization, e.g. char a[3] = {'a', 'b'};
  • Reference initialization, e.g. char& c = a[0];

From time to time, reading about something I need to understand about C++, I run into one of these types, go to this page, and honestly never gain understanding of all those different types. More importantly, should I really care about it?

So far, I was good to write C++ code without understanding different C++ initialization types.

Can somebody provide examples of situations or code snippets, where knowing about initialization types and writing one initialization code over other will make the code works differently (in perforce or any other aspect)? Not considering cases of making useless copies, of course.

0 Answers
Related