Skip to content

Instantly share code, notes, and snippets.

@maxattack
Created February 14, 2014 17:24
Show Gist options
  • Save maxattack/9005161 to your computer and use it in GitHub Desktop.
Save maxattack/9005161 to your computer and use it in GitHub Desktop.
C++11 Unrestricted Union "Slot"
#include <utility>
// Is this mad? The idea is to make a "slot" for an object but have manual initialization/finalization,
// rather than being constrained to C++'s semantics. Useful, e.g., in Object Pools. Is there a simpler
// way to get this effect?
template<typename T>
union Slot {
T inst;
Slot() {}
~Slot() {}
template<typename... Args>
T* init(Args&&... args) { return new(&inst) T(std::forward<Args>(args) ...); }
void release() { inst.~T(); }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment