#include <iostream>
#include <memory>
#include <mutex>
class Singleton {
public:
static Singleton& GetInstance() {
//c++11保证唯一性
static std::once_flag flag;
std::call_once(flag, []() {
instance_.reset(new Singleton);
});
return *instance_.get();
}
~Singleton()
{
std::cout << "~Singleton()" << std::endl;
}
void PrintAddress() const {
std::cout << this << std::endl;
}
private:
Singleton() = default;
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
private:
static std::unique_ptr<Singleton> instance_;
};
std::unique_ptr<Singleton>Singleton::instance_;