Is it valid and well defined (not asking whether it's pretty) to call member functions of an object in global namespace before this object's constructor has been executed (for example from a global new replacement) - and when this member function executes, are the member variables of that object guaranteed to be zero initialized and is it no error to access (and even write to) them? for example:
#include <iostream>
#include <memory>
using namespace std;
struct A {
A() {
new int();
}
};
A a_;
struct B {
B() : var(10) {
}
void print() {
printf("var is %d\n", var);
var = 5;
}
int var;
};
B b_;
void* operator new(size_t bytes) {
b_.print();
b_.print();
return malloc(bytes);
}
int main()
{
b_.print();
return 0;
}
this compiles and prints
var is 0
var is 5
var is 10
but is it defined behaviour?