Skip to content

Instantly share code, notes, and snippets.

@PkmX
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save PkmX/105a27503ef97aaf8ee4 to your computer and use it in GitHub Desktop.

Select an option

Save PkmX/105a27503ef97aaf8ee4 to your computer and use it in GitHub Desktop.
// 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