lundi 25 mai 2015

Schwarz Counter and variadic template

For people who never heard of "Schwarz Counter", please refer to this wikibook page.

I'm trying to implement this idiom with C++11 template, everything works fine except when the template argument ISN'T default constructible. Here is my implementation: (See the code comments for the question)

#include <type_traits>

template<typename T>
class Single {
public:
        static T& get() {
                return reinterpret_cast<T&>(instance.storage);
        }

private:
        struct Instance {
                enum { kSize = sizeof(T), kAlign = std::alignment_of<T>::value };

                int counter;
                typename std::aligned_storage<kSize, kAlign>::type storage;

                template<typename... Arg>
                Instance(Arg&&... param) {
                        if (counter++ == 0) {
                                new (&storage) T(param...);
                        }
                }

                ~Instance() {
                        if (--counter == 0) {
                                reinterpret_cast<T&>(storage).~T();
                        }
                }
        };

        static Instance instance;
};

// The following won't compile if T isn't default constructible.
// I want to get rid of the definition in this case, and let the
// users of this template to provide their own definitions.
// I'd like to keep the it when T is default constructible, because
// most of the time T is (according to my experience, most global 
// objects are constructed without arguments, singletons are
// essentially global objects).
//
// So, basically I want something like this:
// #if std::is_default_constructible<T>::value
//     template<typename T>
//     ...;
// #endif
// Could this be done in C++11 ? If not, how about C++14 ?
template<typename T>
typename Single<T>::Instance Single<T>::instance;

BTW, define a singleton with this template is very simple if the class is default constructible, for example:

class Y {
        friend class Single<Y>;
        Y(int, int) { std::cout << "Y()\n"; }
        ~Y() { std::cout << "~Y()\n"; }
};

class X {
        friend class Single<X>;
        X() { std::cout << "X()\n"; }
        ~X() { std::cout << "~X()\n"; }
};

int main() {
        auto& x = Single<X>::get();
        // auto& g = Single<Y>::get(); // oops
        return 0;
}

Aucun commentaire:

Enregistrer un commentaire