Created
February 5, 2018 21:17
-
-
Save xaxxon/cbca2a4c89b8507231a5bc9418ae96d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Creates an object that is never cleaned up by using placement new into a local memory buffer | |
* Useful for singletons | |
*/ | |
template<class T> | |
struct Immortal { | |
template<class... Args> | |
Immortal(Args&&... args) { | |
::new(space) T(std::forward<Args>(args)...); | |
} | |
operator T&() & noexcept { return reinterpret_cast<T&>(space); } | |
private: | |
alignas(T) unsigned char space[sizeof(T)]; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment