template<class C> class MC {
public:
// do-nothing default constructor
MC() {}
// manual construction: cannot use variadic templates as my version of VC2012 does not support them and i have not confirmed whether the update has XP target support - minor inconvenience anyway.
void construct() { new(_object) C(); }
template<typename T1> void construct(T1 par1) { new(_object) C(par1); }
template<typename T1, typename T2> void construct(T1 par1, T2 par2) { new(_object) C(par1, par2); }
template<typename T1, typename T2, typename T3> void construct(T1 par1, T2 par2, T3 par3) { new(_object) C(par1, par2, par3); }
template<typename T1, typename T2, typename T3, typename T4> void construct(T1 par1, T2 par2, T3 par3, T4 par4) { new(_object) C(par1, par2, par3, par4); }
// manual destruction
void destruct() { ( operator->() )->~C(); }
// would like to overload "." ... but that got stuck in the fucking committee
FORCEINLINE C *operator->() const { return (C*)_object; }
private:
// space for the actual object. (with arbitrary/terrible alignment as thous do not work: "alignas(C)", "__declspec(align(std::alignment_of<C>::value))" )
__declspec(align(16)) byte _object[sizeof(C)];
assertS(std::alignment_of<C>::value <= 16);
// pre-emptively disable nonsense, but might implement later - YAGNI. (does not work: "=delete")
MC &operator=(MC const &);
MC &operator=(MC const &&);
MC(MC const &);
MC(MC const &&);
};
It works - as far as i can see. That is,Test mcTest0(44);
MC<Test> mcTest1, mcTest2;
void testInit() { // called before global variables get constructed
mcTest1.construct();
}
void testExit() { // called after global variables get destructed
mcTest1.destruct();
}
void testMain() { // called in WinMain
mcTest2.construct(77);
}
produces the expected order of events:* constructed: mcTest1, mcTest0, mcTest2
* destructed: mcTest0, mcTest1
Grievances:
* Need to use "->" instead of "." to get to the actual object. Argh, is there any way around it? Perhaps some completely different approach?
* Cannot specify correct alignment. Did i miss an option / workaround for VC2012?
* Darn wrapper again - there is always a chance i missed something. Is there anything that could bite me in future? Any obvious mistakes?
edit:
* It gets darn "funny" to use when the wrapped object also has an overloaded "->". Can not think of anything to alleviate that atm. Ie. the "." vs "->" is even more annoying that i initially thought.