Is this answer considered “good” code or is it just an ugly hack?
And I would like to know how this is forward-declared (both classes).
When I just forward-declare the class with 2 template-parameters, it just always takes this one, no matter what value flag has.
I would like to do this because I have 2 special member functions which should behave differently on flag being true and I don’t feel like reimplementing the whole class. Also, it should have the same name. According to this example, this seems to be possible.
And I have to forward-declare it because I’m creating a library in which we forward-declare everything.
Any idea?
In order to use specialisation its definition always has to be visible to the caller. If, for example, you have
template <class Type, bool flag> struct somethingdefined in one header andtemplate <class Type> struct something<Type, true> : public something<Type, false>defined in the second one, to use the latter you have to include the second header. Without that you will always get the first, more generic type.EDIT: the bit about forward-declaring got me thinking. If you want to use only type declaration, as in pointer variable, do the following:
Header
Source