Singleton in C ++ 11 using member function specifiers

I know the problems with the Singleton pattern , but I wanted to use the exercise to create a Singleton using the C ++ 11 member function qualifiers to learn about C ++ 11.

Anyway, I got to this:

#include<new> #include<vector> class Singleton { private: Singleton() = default; ~Singleton() = default; public: Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; void* operator new(std::size_t) = delete; void* operator new[](std::size_t) = delete; void operator delete(void*) = delete; void operator delete[](void*) = delete; static const Singleton& getInstance() { static Singleton mySingleton; return mySingleton; } }; int main(int argc, const char *argv[]) { const Singleton& s1 = Singleton::getInstance(); // Why does this compile? std::vector<Singleton> v1; // Or this? std::vector<Singleton> v2(50); return 0; } 

And my question is: why these lines:

 std::vector<Singleton> v1; std::vector<Singleton> v2(50); 

to compile and not report a default Singleton constructor error in a private context?

I am using gcc 4.8.2 on a 64bit Linux machine and the code compiles here .

+6
source share
1 answer

Your code does not use the default constructor Singleton outside getInstance, since the vector is empty.

0
source

All Articles