C++ default initialization doesn't zero out variables with auto storage, why the special treatment for static storage variables?
Was it something defined by C and C++ just have to be compatible with? If that's the case why C decides to do zero-initialization?
If a file scope static variables is provided with a initializer, they will be zero-initialized first and then constant/dynamic initialized again. Isn't that redundant? For example the following code 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
}
In this case, why n can't be initialized to argc directly?
EDIT: Part of this question has been answered by the question here: Static variable initialization? But I don't think it's a duplicate because the answers in the other question didn't answer my second question, ie. why the 2 staged initialization. Besides, the title of the other post doesn't really say what exactly the question is.