Last active
August 29, 2015 14:18
-
-
Save PkmX/105a27503ef97aaf8ee4 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
| // Compile with clang++ -std=c++14 | |
| #include <functional> | |
| struct foo { | |
| // Implicitly compiler-generated default destructor | |
| // ~foo() { } | |
| }; | |
| template<typename T> | |
| void make_t(const std::function<auto (T&) -> void> f) | |
| { | |
| // Allocate space for T | |
| alignas(T) char c[sizeof(T)]; | |
| T* t = reinterpret_cast<T*>(c); | |
| // Placement new | |
| new (c) T(); | |
| // Use T, assuming no exception | |
| f(*t); | |
| // Destruct T | |
| t->~T(); | |
| } | |
| int main() | |
| { | |
| make_t<foo>([](auto){}); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment