Created
February 14, 2014 17:24
-
-
Save maxattack/9005161 to your computer and use it in GitHub Desktop.
C++11 Unrestricted Union "Slot"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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