Consider the following C++ code:
#include <iostream>
#include <string>
#define DUMP(_, str) do { \
std::cout << str; \
for (int i = 0; i < 10; ++i) \
std::cout << (_).x[i] << std::endl; \
std::cout << "a = " << (_).a << std::endl; \
std::cout << "b = " << (_).b << std::endl; \
std::cout << "c = " << (_).c << std::endl; \
std::cout << "d = " << (_).d << std::endl; \
std::cout << std::endl; \
} while (0)
//#define ENABLE_WTF
struct A {
int x[10];
float a, b, c, d;
#ifndef ENABLE_WTF
A() : d(4) { DUMP(*this, "=== INSIDE CTOR ===\n"); }
#else
A() : d(4) {}
#endif
};
int main() {
A a;
DUMP(a, "=== OUT OF CTOR ===\n");
}
As can be seen, A
has a partial constructor that only initializes one of the fields, whereas all the other fields predictably remain garbage; demonstration here.
And now the question: Is it compiler-specific to zero-out the rest of A
when its partial constructor has no body (demonstration here), or is that a part of C++ itself?