C++ Initialization types – is there a practical use? [duplicate]

  c++, c++11, c++20, initialization

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.

I want to ask, is there any practical gain to know and understand all of those types? Are there situations where I can write better code, or make better decisions, based on this knowledge? Can you show examples of such situations or code snippets, where knowing about initialization types will matter.

Source: Windows Questions C++

LEAVE A COMMENT