0

If we have

// Uninitialized
MyStruct s;

// Direct list initialization
MyStruct s2{};

What are the differences?

My only guess here is that s would have all of its members in an undefined state, where s2 has everything zeroed out based on what I've read online. I have no idea if this is true, and I'm not finding the answer by looking through the cppreference (most likely I missed it).

However I am unsure whether or not MyStruct s2{} actually "zeros everything out" or whether it is identical to MyStruct s and what I am saying above is wrong. Not sure what the difference is and what exactly is going on under the hood with these two statements. Further, if they do the exact same thing, then is one preferred over the other?

Water
  • 3,245
  • 3
  • 28
  • 58
  • 2
    `MyStruct s2{}` need not zeros everything out. https://wandbox.org/permlink/HSQhEtkSMsskFwZS – MikeCAT Jul 08 '21 at 13:29
  • related/dupe: https://stackoverflow.com/questions/5610629/difference-between-default-initialize-and-value-initialize – NathanOliver Jul 08 '21 at 13:30
  • Is it fair to assume that you are aware the question only applies to trivially-constructible types? –  Jul 08 '21 at 13:32
  • @Frank Right now I'm coming back to C++ after having not done it for years. When you say trivially constructible, I know what you mean as the memories are coming back, but it was something I forgot about when writing this. It would be safe to assume this is the case for other things, or maybe any answer you give should err on the side of caution. – Water Jul 08 '21 at 13:35

0 Answers0