I was trying to solve a problem, but found a different solution. however out of curiosity like to know if the following is possible:
template< class > struct S;
template< > struct S< Foo > : struct< Foo > {};
I would like to be able to inherit nonspecialized struct from specialized struct.the example above does not work because the inherited struct is the specialized, leading to the infinite recursion.
One possible solution was to add second template parameter, say bool specialized, such that the default is false, and specialized template has that parameter is true.however that makes things a bit messy since instantiation needs to specify additional parameter.
Is there some other way around to implement the above?
the original problem was to implement matrix of matrixes, where matrix itself may have additional operators, depending if the constituent matrixes has those operators.I would hope that makes sense. at the same time different specialized matrix need to be of the same base class at the same time retaining the same name, although with different template parameters. I have thought there might be a way to do it using enable_if and type traits
You could keep all the generic stuff in a separate type, and extend that with your specialisation:
Edit: Alternatively, if you don't like the extra name, the way to use an extra flag without messiness when instantiating the template is to use a default value:
I like Mike Seymour's answer but you don't really have to specify true and false everywhere.
You can have something like this:
We only need to default the
Object
template classbool
tofalse
, then when we instantiate aObject<SpecialObject>()
it now has assigned to it the full type which isObject<SpecialObject, false>
so now all we need to do is tell the compiler to find a potential different specialization that matchesObject<SpecialObject, true>
and we do that after the:
above.Now the compiler will choose the class with the
abstract_method
in it, even though it was defaulted to false, we already have chosen that specialization withfalse
because of that default parameter when the compiler first encountered the typeObject<SpecialObject>
it is really the same asObject<SpecialObject, false>
, so now when we passtrue
we are explicitly saying I don't care who took use of that defaultfalse
parameter now try to find a different specialization usingtrue
, and it works.You can do
template<class Foo, bool flag = false>
, so the second parameter is optional.