Consider the following code:
aligned_storage<sizeof(T)> buffer;
T& ref(*reinterpret_cast<T*>(&buffer));
new (&buffer) T();
Use(ref);
The context for this is refactoring some global objects into ones which are explicitly initialized (as opposed to initialized by the compiler during static initialization), without affecting existing users (and without using macros).
I think that code causes undefined behaviour as written, but is there any valid (portable) way of accomplishing the same thing? The closest thing I can think of is to use some type with operator T&
, but that could still break users.