7

I am working on an n-ton base class template. I don't worry about laziness yet, so the Intent is:

Ensure a class has only n instances, and provide a global point of access to them.

Here is my code so far:

template<typename Derived, size_t n = 1>
class n_ton_base                 // Singletons are the default
{
    static Derived instances[n + (n == 0)];
                              // Zerotons are supported, too
protected:

    // Prevent n_ton_base to be used outside of inheritance hierarchies
    n_ton_base() {} 

    // Prevent n_ton_base (and Derived classes) from being copied
    n_ton_base(const n_ton_base&) = delete;

public:
                   // Get first element by default, useful for Singletons
    template<size_t i = 0>
    static Derived& get_instance()
    {
        static_assert(i < n, "Time to increase n it seems!");
        return instances[i];
    }
};

And here is how one would use it:

class SingletonExample : public n_ton_base<SingletonExample>
{
public:
    void method()
    {
        std::cout << "Singletons are overused.\n";
    }    
};

class DoubletonExample : public n_ton_base<DoubletonExample, 2>
{
public:
    void method()
    {
        std::cout << "Doubleton " << this << " says hello.\n";
    }    
};

int main()
{
    SingletonExample::get_instance().method();
    DoubletonExample::get_instance().method();
    DoubletonExample::get_instance<0>().method();
    DoubletonExample::get_instance<1>().method();
}

Unfortunately, the code doesn't compile yet:

/tmp/ccsFtliS.o: In function `SingletonExample& n_ton_base<SingletonExample, 1ul>::get_instance<0ul>()':
nton.cpp:(.text._ZN10n_ton_baseI16SingletonExampleLm1EE12get_instanceILm0EEERS0_v[SingletonExample& n_ton_base<SingletonExample, 1ul>::get_instance<0ul>()]+0x5): undefined reference to `n_ton_base<SingletonExample, 1ul>::instances'
/tmp/ccsFtliS.o: In function `DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<0ul>()':
nton.cpp:(.text._ZN10n_ton_baseI16DoubletonExampleLm2EE12get_instanceILm0EEERS0_v[DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<0ul>()]+0x5): undefined reference to `n_ton_base<DoubletonExample, 2ul>::instances'
/tmp/ccsFtliS.o: In function `DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<1ul>()':
nton.cpp:(.text._ZN10n_ton_baseI16DoubletonExampleLm2EE12get_instanceILm1EEERS0_v[DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<1ul>()]+0x5): undefined reference to `n_ton_base<DoubletonExample, 2ul>::instances'
collect2: ld gab 1 als Ende-Status zurück

What did I do wrong?

fredoverflow
  • 237,063
  • 85
  • 359
  • 638
  • n_ton_base::instances is static, so you should define it somewhere. At the moment you are only declaring it – Tom Knapen Feb 02 '13 at 14:09
  • You haven't added a definition for the static array? – Andy Prowl Feb 02 '13 at 14:09
  • 11
    I vote for this as douchey design pattern of the year. –  Feb 02 '13 at 14:18
  • 1
    It does compile actually, this is a linker error revealing a missing symbol. – Matthieu M. Feb 02 '13 at 14:24
  • 2
    I think it would be easier to create the static array n_instances in the function get_instance() rather than to declare it as a static member. See the C++ example of the "multiton pattern" on wikipedia. – Étienne Feb 02 '13 at 14:28
  • 2
    "Industrial-strength" sounds a bit BS (although I do understand what you mean, this phrase is mostly found in commercials). – Tamás Szelei Feb 02 '13 at 15:47

3 Answers3

9

As Etienne Cordonnier pointed out, it is much easier to use a local static instead of a class static:

template<typename Derived, size_t n = 1>
class n_ton_base                 // Singletons are the default
{
protected:

    // Prevent n_ton_base to be used outside of inheritance hierarchies
    n_ton_base() {} 

    // Prevent n_ton_base (and Derived classes) from being copied
    n_ton_base(const n_ton_base&) = delete;

public:
                   // Get first element by default, useful for Singletons
    template<size_t i = 0>
    static Derived& get_instance()
    {
        static_assert(i < n, "Time to increase n it seems!");

        static Derived instance;
        return instance;
    }
};

Note that each instantiated member function will have its own local static, so no array is needed.

This also achieves thread-safe lazy initialization without me having to do anything about it. Nice!

Community
  • 1
  • 1
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
8

Add this at global scope:

template<typename Derived, size_t n>
Derived n_ton_base<Derived, n>::instances[n + (n == 0)];

Btw, std::array<> allows zero-size arrays, so you might want to consider it.

Andy Prowl
  • 114,596
  • 21
  • 355
  • 432
  • Interesting that you edited the `= 1` part away, how is that relevant? – fredoverflow Feb 02 '13 at 14:13
  • @FredOverflow Default arguments are only allowed in the declaration or in the initial definition. – 0x499602D2 Feb 02 '13 at 14:13
  • @FredOverflow: I removed it because default arguments cannot be overridden in a definition (and that's a definition). It works the same as with functions with default arguments: you can't add them both to the declaration and to the definition of a function, even though they are consistently specified. Initially I copy pasted the template argument list and forgot to remove it. – Andy Prowl Feb 02 '13 at 14:15
  • @David: We have to do this exactly because it is consider a declaration inside the class, and not a definition. Thus, we need to add a definition. – Andy Prowl Feb 02 '13 at 14:18
  • How does `std::array` allow zero-size arrays? A similar hack, or partial specialization? – fredoverflow Feb 02 '13 at 14:19
  • @FredOverflow: pardon, fixed the typo. The way this is done is implementation-defined, I guess. The standard only specifies that 0 is a valid size. But yes, most likely they all use a similar hack. libstdc++ uses `instances[n ? n : 1]` – Andy Prowl Feb 02 '13 at 14:21
1

You can find here an explanation about class static members:

Static data members (C++ only):

The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope. For example:

class X {
public:
    static int i;
};

int X::i = 0; // definition outside class declaration

Therefore you have to define n_ton::instances outside of your class.

Community
  • 1
  • 1
Étienne
  • 4,131
  • 2
  • 27
  • 49