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 开发者_StackOverflow中文版> 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:
template <typename> struct S_generic { /* generic stuff here */ };
template <typename T> struct S : public S_generic<T> { /* nothing here */ };
template <> struct S<Foo> : public S_generic<Foo> { /* extra stuff here */ };
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:
template <typename T, bool fully_defined=true> struct S;
template <typename T> struct S<T,false> { /* generic stuff here */ };
template <typename T> struct S<T,true> : public S<T,false> {};
template <> struct S<Foo,true> : public S<Foo,false> { /* extra stuff here */ };
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.
You can do template<class Foo, bool flag = false>
, so the second parameter is optional.
I like Mike Seymour's answer but you don't really have to specify true and false everywhere.
You can have something like this:
template<class T, bool = false>
class Object {
void abstract_method();
};
// Now for your derived specialized type do this
template<>
class Object<SpecialObject> : Object<SpecialObject, true> {
// override any methods
};
We only need to default the Object
template class bool
to false
, then when we instantiate a Object<SpecialObject>()
it now has assigned to it the full type which is Object<SpecialObject, false>
so now all we need to do is tell the compiler to find a potential different specialization that matches Object<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 with false
because of that default parameter when the compiler first encountered the type Object<SpecialObject>
it is really the same as Object<SpecialObject, false>
, so now when we pass true
we are explicitly saying I don't care who took use of that default false
parameter now try to find a different specialization using true
, and it works.
精彩评论