#include #include class Foo { public: Foo(int e) { a = e; } ~Foo() { if (a) { a = 0; } } private: int a; }; struct FooTlsDeleter { void operator()(Foo* object) { if (object == nullptr) { return; } object->~Foo(); } }; extern "C" void foo_ctor() { thread_local std::unique_ptr foo(nullptr); foo = std::unique_ptr(new Foo(1)); }