// file x.h
struct X {
int data;
};
// file y.h
#include <cstddef>
class Y {
public:
Y();
~Y();
/*...*/
private:
static const size_t sizeofx = 8;
char _x[sizeofx];
};
// file y.cpp
#include <new>
//#include "y.h"
//#include "x.h"
Y::Y() {
// compile-time check
static_assert(sizeofx >= sizeof(X), "sizeofx too small");
// does not allocate memory, but constructs an object at &_x[0]
new (&_x[0]) X;
}
Y::~Y() {
(reinterpret_cast<X*>(&_x[0]))->~X();
}
// file main.cpp
//#include "y.h"
int main()
{
Y y;
return 0;
}
Ly8gZmlsZSB4LmgKc3RydWN0IFggewogIGludCBkYXRhOwp9OwoKCi8vIGZpbGUgeS5oCiNpbmNsdWRlIDxjc3RkZGVmPgoKY2xhc3MgWSB7CnB1YmxpYzoKICBZKCk7CiAgflkoKTsKICAvKi4uLiovCnByaXZhdGU6CiAgc3RhdGljIGNvbnN0IHNpemVfdCBzaXplb2Z4ID0gODsKICBjaGFyIF94W3NpemVvZnhdOwp9OwoKCi8vIGZpbGUgeS5jcHAKI2luY2x1ZGUgPG5ldz4KLy8jaW5jbHVkZSAieS5oIgovLyNpbmNsdWRlICJ4LmgiCgpZOjpZKCkgewogIC8vIGNvbXBpbGUtdGltZSBjaGVjawogIHN0YXRpY19hc3NlcnQoc2l6ZW9meCA+PSBzaXplb2YoWCksICJzaXplb2Z4IHRvbyBzbWFsbCIpOwogIC8vIGRvZXMgbm90IGFsbG9jYXRlIG1lbW9yeSwgYnV0IGNvbnN0cnVjdHMgYW4gb2JqZWN0IGF0ICZfeFswXQogIG5ldyAoJl94WzBdKSBYOwp9CgpZOjp+WSgpIHsKICAocmVpbnRlcnByZXRfY2FzdDxYKj4oJl94WzBdKSktPn5YKCk7Cn0KCgovLyBmaWxlIG1haW4uY3BwCi8vI2luY2x1ZGUgInkuaCIKCmludCBtYWluKCkKewogIFkgeTsKICByZXR1cm4gMDsKfQ==