[LC++]Static polymorphism and arrays of poly. objects

Dan hursh hursh at sparc.isl.net
Tue Aug 21 01:44:41 UTC 2001


	This is a case where some of the work will almost certainly at
runtime (I think).  You are right that everything that needs to be known
at runtime is there, but the way you want to do it (with a polymorphic
pointer) would hide the information from the compiler. 
	By handing it the pointer, you are telling the compiler that you
could pass anything that fits the definition of the base class.  Worse
yet, you didn't define the operator() as virtual (those aren't implicit
right) so you won't get the right runtime behavior either.  Wow.  This is
a mess.  
	To be honest, I think the correct generic way of doing this would
require the ability to define template functions that take variable number
of template arguments.  In any case, it will take someone better at
template magic than me.  I would want to use runtime polyporphism here.

Dan


On Mon, 20 Aug 2001, Mark Phillips wrote: 

> Suppose I am using templates to implement static polymorphism.
> For example, suppose I have the following base type:
> 
> template<typename functionLeafType>
> class functionBaseTy {
>  public:
>     functionLeafType& asLeaf() {
>       return static_cast<functionLeafType&>(*this);
>     }
>     int operator()(int i) {return asLeaf()(i);}
> };
> 
> This is the Barton and Nackman Trick for doing static polymorphism.
> It allows us to statically define a whole lot of leaf "function" 
> classes, and yet refer to them via a base class.
> 
> Example leaf "function" classes might include:
> 
> struct squareTy: public functionBaseTy<squareTy> {
>   int operator(int i) const {return i*i;}
> };
> 
> struct twiceTy: public functionBaseTy<twiceTy> {
>   int operator(int i) const {return 2*i;}
> };
> 
> struct thriceTy: public functionBaseTy<thriceTy> {
>   int operator(int i) const {return 3*i;}
> };
> 
> Then you can write the following generic algorithm:
> 
> template<typename functionLeafType>
> int apply(functionBaseTy<functionLeafType>& f, int i) {
>   return f(i);
> };
> 
> and then write:
> 
> squareTy square;
> apply(square, i);
> twiceTy twice;
> apply(twice, i);
> thriceTy thrice;
> apply(thrice, i);
> 
> This algorithm "apply" is pretty basic, but obviously
> you could have all sorts of generic algorithms.
> 
> 
> But what happens if I want to have an __array__ of
> these leaf "function" objects?
> 
> Ie suppose instead of wanting to write a generic algorithm
> taking one functionLeafType as a parameter, I want it
> to take an array of these leaf objects, where the size of
> this array, and the objects that are to go in it, are known
> at compile time?
> 
> Well actually, in order for array addressing to work properly,
> we will need an array of "constant" pointers to these objects.
> For example, I might want an array with three elements in it,
> these being square, twice and thrice.  I know everything at
> compile time, so I should be able to have a generic algorithm
> which takes as an argument, an array of pointers to these
> objects, and then pass this array with its three elements, into
> this algorithm.  Everything should be able to be handled at
> compile time.
> 
> The problem is, I can't yet see how it would be done.
> 
> My thoughts are that it would be something along the
> following lines:
> 
>   functionBaseTy* myArray[] = {squareTy(), twiceTy(), thriceTy()}
>   
>   template<functionBaseTy* leafArrayType[]>
>   void myAlgorithm......
> 
> but the first line doesn't work and I doubt that the template
> line works either.
> 
> Any ideas?
> 
> Thanks,
> 
> Mark.
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
> 




More information about the tuxCPProgramming mailing list