1 #include <iostream> 2 #include <memory> 3 4 class Foo { 5 public: Foo(int e)6 Foo(int e) 7 { 8 a = e; 9 } ~Foo()10 ~Foo() 11 { 12 if (a) { 13 a = 0; 14 } 15 } 16 private: 17 int a; 18 }; 19 20 struct FooTlsDeleter 21 { operator ()FooTlsDeleter22 void operator()(Foo* object) 23 { 24 if (object == nullptr) { 25 return; 26 } 27 object->~Foo(); 28 } 29 }; 30 foo_ctor()31extern "C" void foo_ctor() 32 { 33 thread_local std::unique_ptr<Foo, FooTlsDeleter> foo(nullptr); 34 foo = std::unique_ptr<Foo, FooTlsDeleter>(new Foo(1)); 35 } 36