A common construct for C++ programmer is a template called Singleton.
/**
* Singleton design pattern class
**/
template<class SINGLECLASS>
class Singleton
{
public:
static SINGLECLASS& instance();
protected:
inline Singleton(){}
inline ~Singleton(){}
};
template<class SINGLECLASS>
SINGLECLASS& Singleton<SINGLECLASS>::instance()
{
static SINGLECLASS s_instance;
return s_instance;
}
class MyClass : public Singleton<MyClass>
{
public:
MyClass() {}
};
Now, if I call MyClass::instance() from different libraries and the executable (probably different optimizations), the compiler/linker forces inlining
and I can observe multiple constructor calls at the line "static SINGLECLASS s_instance" which is supposed to happen just once per template parameter (SINGLECLASS) type.