-1

I have a triple hierarchy class:

template<class T> class Singleton;

class Base;

class Sub : public Base, public Singleton<Sub>;

I' using underlying auto pointers, that's why Singleton is a template class and Sub passes itself as a template parameter. I'm developing Singleton and Base and a public API allows anyone to add their own sub classes. I actually want a real triple hierarchy like this:

template<class T> class Singleton;

class Base : public Singleton<Base>;

class Sub : public Base;

So that external developers don't have to worry about templates and complexity. The problem with this is that my implementation in Singleton will now call the constructor of Base whenever I create an instance of Sub (since the template parameter is Base).

I was wondering if this could be done by pre-processor macros:

template<class T> class Singleton;

class Base : public Singleton<__CLASS_NAME__>;

class Sub : public Base;

Where __CLASS_NAME__ is the class name that will be replaced by the pre-processor. Theoretically this should be possible, since the __PRETTY_FUNCTION__ macro actually returns the class name. The problem is that one cannot do string-manipulation to remove the function name from __PRETTY_FUNCTION__.

Any ideas on how I can accomplish this so that the Sub class is not aware of inheriting from a Singleton<template> class?

Rapptz
  • 20,807
  • 5
  • 72
  • 86
goocreations
  • 2,938
  • 8
  • 37
  • 59
  • Do you mean not aware of inheriting a `template` or not aware of inheriting a `singleton`? – Peter Wood Mar 08 '13 at 09:02
  • 3
    "`template class Singleton;`" -- you're bad and you should feel bad. Print all lines of your code with the word "singleton" in it and burn them. – Xeo Mar 08 '13 at 09:02
  • 3
    `$ grep -Rli singleton . | xargs shred` – R. Martinho Fernandes Mar 08 '13 at 09:07
  • @R.MartinhoFernandes If only it were that simple. Although it's not that difficult either. Just make one and pass it around. Makes explicit order of creation issues. We have a system where it's easy to mistakenly create an infinite singleton instantiation loop. I was going to say if you're not really, really careful, but actually being really, really, really careful doesn't help. It's almost impossible to think about, like hitting a singularity in the middle of your normal test/code/refactor cycle. – Peter Wood Mar 08 '13 at 10:14

2 Answers2

8

So that external developers don't have to worry about templates and complexity.

Impossible. The only way that you can design Base and Singleton to remove the requirement to pass a template parameter to Singleton is to instead pass it to Base, which is no improvement.

In addition, it's reasonably expectable that a C++ developer will be able to use the CRTP.

Finally, Singletons suck in the most horrific fashion. If you value your sanity, remove immediately.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 2
    I'm happy to see that my intense hatred of singletons is shared by people a lot smarter than me ;-) – Joris Timmermans Mar 08 '13 at 09:07
  • 1
    @MadKeithV I'm with you but be careful of [confirmation bias](http://en.wikipedia.org/wiki/Confirmation_bias) and an [appeal to authority](http://en.wikipedia.org/wiki/Argument_from_authority). – Peter Wood Mar 08 '13 at 09:09
  • Thanks for the reply. But I have to disagree with: "Singletons suck in the most horrific fashion". If you really understand design patterns and specifically the Singleton, you will know that it can drastically improve your code quality, object creation and unviversal access of an object. – goocreations Mar 08 '13 at 10:24
  • 3
    If you really understand Design Patterns you will know that, in retrospect, the authors wished they'd left out `Singleton`. – Peter Wood Mar 08 '13 at 10:48
  • 4
    @goocreations I think your primary problem is that you really haven't understood all the implications of using a singleton, and therefore are making such unsubstantiated claims. – Tony The Lion Mar 08 '13 at 11:06
  • 1
    @goocreations: Universal access of an object is usually a *horrifically bad thing*. Singletons are the worst design pattern imaginable and the guys who wrote the design patterns book, for whom I have little respect anyway, explicitly stated that they wished they had left it out. – Puppy Mar 08 '13 at 11:07
  • 1
    @goocreations I admire TonyTheLion for being able to put it so mildly today. Really. What Peter said. Don't take anything on authority: work it out for yourself. See what works and what _value_ singletons add. Cheers – sehe Mar 08 '13 at 11:08
  • One of the problems with singletons is people confuse 'I only need one instance of this' with 'I cannot possible comprehend more then one instance of this'. If you only need one instance, only create one. If the world will implode because a second instance was created, then you need to rethink the design of your solution. And if a developer using your code can't work out how to pass a reference around, they have bigger issues to sort out. And no, this is not all that is wrong with singletons. – thecoshman Mar 08 '13 at 11:13
  • My original message was intended for figuring out templates. I admit I'm not a expert when it comes to templates, but doing academic research on the Singleton DP for the past 2 years, I must say either you people don't understand the Singleton or you were taught improperly (something I notice at a lot of Universities/Colleges these days). I suggest you read Andrei Alexandrescu's books and papers. Anyway, thanks for the help. Seems like my solution with the templates won't work. – goocreations Mar 08 '13 at 12:18
  • @goocreations Andrei's book is wonderful, and he implements, what is it, 24 different singletons? His design sense is acute, in terms of identifying orthogonal concerns and developing policies. I love his writing. It doesn't make using singletons right, though. – Peter Wood Mar 08 '13 at 13:20
2

I am not that familiar with the CRTP (http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern) but what about making Base itself a template?

template<class T> class Singleton;

template<class T> class Base : public Singleton<T>;

class Sub : public Base<Sub>;

Martin Komischke
  • 1,440
  • 1
  • 13
  • 24
  • Yes, thought of that. But this creates problems with polymorphism. Now Sub and Base are not polymorphic if you use them in a third template class. Eg: MyPointer t = MyPointer(); – goocreations Mar 08 '13 at 10:22