I started learning c++ . Can anyone explain in detail how these initialization options differ? When what is called, etc.?

  Type x = smth ; Type x (smth); Type x = {smth}; Type x {smth}; Type x = (smth); 
  • Much depends on what Type is. :) Read this link at the end of the topic about differences in initialization. cpp.forum24.ru/?1-3-0-00000047-000-10001-0-1475597138 - Vlad from Moscow
  • @VladfromMoscow to me at a more primitive level ... - user226016
  • Here's some more related topic: ru.stackoverflow.com/q/242499/10105 - VladD
  • @ user226016 Just in the link that I provided in the message that contains the heading "Metamorphoses of Initialization" is written without digging into details, and the main differences are highlighted. This topic is really big, so your question is too broad. - Vlad from Moscow
  • @VladD thanks! and for curly braces? - user226016

1 answer 1

Foreigners had a discussion here. Initialization difference with or without Curly braces in C ++ 11

In short, initialization {} will not allow us to perform "narrowing" transformations. That is, you can not assign int thus char

  • an int char can be assigned if it does not require a narrowing conversion. - ixSci
  • one
    so I wrote about this, you can char i = {22}, you can unsigned char i = {222}. But you can not char i = {222}, although char i = 222 will work. - Majestio
  • It's just that you have written that “you cannot assign an int to a char in this way” , and this is not the case, as you yourself have shown: char i ={22} - the assignment of int to char is done here. I understand what you had in mind, but in the current wording this is not entirely correct. - ixSci
  • ... will not allow to produce " those who narrow down " .... in such a way ... Well, I don’t know, probably "in this way" it was necessary to single out it somehow. I thought, and so it will be clear. - Majestio