In the following example:
#include <iostream>
struct A {
int z;
A(std::string) {}
A() = default;
};
int main() {
char buf[1000];
std::fill(buf, buf + 1000, 'x');
auto a = new(buf) A{};
std::cerr << "A.z: " << a->z << std::endl;
}
Compiled with GCC 4.8 outputs zero (same behavior with Clang 3.4). This seems to indicate that a
is being zero-initialized before the default constructor is called.
But according to value-initialization rules on cppreference.com, the object should not be initialized before the default constructor invocation. Class A
qualifies for bullet point #1 under C++11:
1) If T is a class type with at least one user-provided constructor of any kind, the default constructor is called.
Another perhaps useful data point is that if we replace A() = default
with A() {}
in the above example, no zero-initialization happens as expected. This seems to indicate that bullet point #2 of value initialization is being followed in the initial example which would be wrong:
2) If T is an non-union class type without any user-provided constructors, then the object is zero-initialized and then the implicitly-declared default constructor is called (unless it's trivial)