dimanche 7 juillet 2019

Initialisation of a std::string seems to generate the same code no matter which initialisation I use (uniform, assignment, etc)

I was experimenting with C++ Insights and I got a result that was surprising to me and which I don't really understand. I was hoping some one can provide some illumination for.

Given this code snippet:

#include <string> 

int main()
{
  std::string a {"123"};
  std::string b = {"123"};
  std::string c = "123";
  std::string d("123");
}

I was at least expecting a to be initialised differently to c. With c I was expecting some sort of temporary string to be copied and for a I was expecting just the constructor to be called directly.

Here is the link to c++ insights: here (You have to press the "play" button).

Every one of the different ways to initialise a string is the same. This really surprised me. I started with c++17 and then switched to c++11, which produced something more like what I was expecting.

Does this mean that all init types in c++17 are now the same? - is there a name for this, because I thought uniform init was only with the curly braces {} - is everything now uniform init?

Aucun commentaire:

Enregistrer un commentaire