Reputation: 5249
The following code example is from cppreference: http://en.cppreference.com/w/cpp/language/zero_initialization
#include <string>
double f[3]; // zero-initialized to three 0.0's
int* p; // zero-initialized to null pointer value
std::string s; // zero-initialized to indeterminate value
// then default-initialized to ""
int main(int argc, char* argv[])
{
static int n = argc; // zero-initialized to 0
// then copy-initialized to argc
delete p; // safe to delete a null pointer
}
It says std::string is zero initialized to indeterminate value. The same page also says for a non-union class type, all non static members are zero initialized.
Without knowing the implementation details of std::string, I would think it has a member of char* which stores the actual string value. If that's the case, shouldn't the char* be zero initialized to null pointer, and if so then why the value is indeterminate?
Upvotes: 1
Views: 1537
Reputation: 145289
The basic constituents are zero-initialized, but what those zero-values mean for std::string
, if anything, depends entirely on the implementation.
The dynamic initialization invokes the std::string
constructor and establishes the class invariant (the basic assumptions about the internal state of the instance).
Only after that are the values such that the object is guaranteed usable.
Upvotes: 3