2007年6月8日星期五

C++ template specialisation help

 
We have a template:

template<class Type>
class t_type : public BaseNode.

In this template, we've defined generic behaviours for amany functions..


Now, we're defined a specialisation, so we can control how it behaves with these types:


template<>
class t_type<mpz_class> : public BaseNode

We also have:


template<>
class t_type<mpf_class> : public BaseNode


Note: mpf and mpz are classes from GNU MP. (Multi Precision library, allows arbituary sized #'s.


Now, I have a function in the mpz specialisation, that must return a new instance of the mpf specialisation. Note that the mpf specialisation is declared below the mpz's , and NOR does a forward declaration help.


When i go to compile said function, i get:

my_type.h:2009: error: specialization of
#t_type<__gmp_expr<__gmpf_value, __gmpf_value> ># after instantiation
my_type.h:2009: error: redefinition of #class
t_type<__gmp_expr<__gmpf_value, __gmpf_value> >#
my_type.h:43: error: previous definition of #class
t_type<__gmp_expr<__gmpf_value, __gmpf_value> >#


what the heck? The line it is pointing to, for the redefinition, is the 'return new' statement.

Obviously: its because I am trying to instanciate something i have specialised on below it.
So, move it up?

Cant: I need to be able to create new instances of all specialised classes.

Possible solution:
forward declaration: still yeilds same result.

Note: the generic class should in itself, act as the 'header' for the function, and indeed it does, as it the 'redefinition complaint' shows.

How can i get it to allow me to instantiate it?


Rationale for creating the new instances: As you can see, the classes all inherit from the class BaseNode, BaseNode is used to attach these to a tree. We need to be able to attach different classes to a tree, each with the same functions, and have them behave differently. (Each specialisation has functions that will tell us about it, and also, convert between different types).

-----
Remember kids, they aren't H games, they're "Bishoujo Games."

 

没有评论: