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 .
source share