C++ singleton with policies

The traditional Singleton pattern only supports a default constructor (one that takes no arguments), but recently I needed to create a singleton from a class which required an integer argument in its constructor.

This can be solved quite elegantly with a policy-based design. Take a look at the following (non thread-safe!) implementation:

#ifndef SINGLETON_HPP
#define SINGLETON_HPP

// Creation policies
template<class T> struct CreateWithIntParameter
{
    static T* Create()
    {
        return new T(m_intParameter);
    }
    static const int m_intParameter;
};

// Non-thread-safe singleton
template <class T, template<class> class CreationPolicy>
class Singleton
{
public:
    static T& instance()
    {
        if (!m_instance)
        {
            m_instance = CreationPolicy<T>::Create();
        }
        return *m_instance;
    }
private:
    Singleton();
    Singleton(const Singleton&);
    Singleton& operator=(const Singleton&);

    static T* m_instance;
};

template <class T, template<class> class C>
T* Singleton<T, C>::m_instance = 0;

#endif // SINGLETON_HPP

Here is a small example how to use it:

#include "singleton.hpp"
#include <iostream>

using namespace std;

class Foo
{
public:
    Foo(int a) : m_a(a)
    {
        cout << "Foo constructed" << endl;
    }
    void bar()
    {
        cout << "a = " << m_a << endl;
    }
private:
    Foo();
    int m_a;
};

template<> const int CreateWithIntParameter<Foo>::m_intParameter = 5;

typedef Singleton<Foo, CreateWithIntParameter> FooSingleton;

int main()
{
    FooSingleton::instance().bar();
    FooSingleton::instance().bar();
    return 0;
}

The output looks like this:

Foo constructed
a = 5
a = 5

ClearCase and text files with long lines of text

Note 2 myself just in case I’ll stumble into this again:

I’ve tried to add a javascript file (jquery-1.3.2.min.js) to clearcase, and got the following response:

jquery-1.3.2.min.js is not a 'text file': it contains a line exceeding 8000 bytes. Use a different type manager (such as compressed file).
Type manager "text_file_delta" failed create_version operation.

Huh? Anyway, this can be solved by

$ cleartool chtype -nc compressed_file jquery-1.3.2.min.js


Change version manager and reconstruct all version for "jquery-1.3.2.min.js"? [no] y
Changed type of element "jquery-1.3.2.min.js" to "compressed_file".

Afterwards, the file can be checked in without problems.