Hacker News new | ask | show | jobs
by raydev 1515 days ago
But that would compile and run, and you'd still get garbage if you tried to access str later, correct?

In Swift you are not allowed to declare a variable without assigning some value or nil to it before the current scope ends.

3 comments

I don't think that's true. The default constructor for std::string creates an empty string. See: https://www.cplusplus.com/reference/string/string/string/

The semantics of c++ object initialization are such that a constructor will always be called.

No, either you get the previous content of the string (if it was under the small-string optimization limit) or a new empty string, but never garbage.
In C++, you can prevent that with compiler options.