3 ms·
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 witho
by raydev 4y 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.
- benchaney 4y agoI 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/ https://www.cplusplus.com/reference/string/string/string/ The semantics of c++ object initialization are such that a constructor will always be called.
- jcelerier 4y agoNo, 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.
- pjmlp 4y agoIn C++, you can prevent that with compiler options.