C++: Templates and the singleton pattern
问题 It happens so that I have a need of the infamous singleton pattern. Better yet, it happens so that I have a need of infamous C++ templates in combination with that pattern. So, what troubles me is this: template <class T> class PDatabaseTable { ... static PDatabaseTable <T> & instance() { static PDatabaseTable <T> singleton; return singleton; } ... }; This is a typical way to implement a singleton that's supposed to be created on the first use. Now, here we have a static variable singleton .