Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ specialized template inherit from non-specialized version

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

like image 404
Anycorn Avatar asked Jan 13 '10 20:01

Anycorn


People also ask

Are template specializations inline?

An explicit specialization of a function template is inline only if it is declared with the inline specifier (or defined as deleted), it doesn't matter if the primary template is inline.

Can template class inherit?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.

When we specialize a function template it is called?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is Typename in C++ template?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.


2 Answers

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 */ };
like image 85
Mike Seymour Avatar answered Sep 26 '22 01:09

Mike Seymour


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.

like image 23
sepp2k Avatar answered Sep 25 '22 01:09

sepp2k