Ok from you reply [paragraphs please!!] You haven't been that clear.
So I am going to guess what the problem is and what is in my opinion the optimal solution.
First : The difference between a a couple of template instances e.g.
class Image
{
public:
void setPixel(const int,const int,const RGB&);
void setPixe(const int,const int,const Grey&);
};
and
class Image
{
public:
template<typename T>
void setPixel(const int,const int,const T&);
};
is relatively little. Both are resolved in the same way (assuming that you have instances, were T is RGB and T is Grey).
However, templates can be used for a return value.
class Image
{
template<typename T>
T getPixel(const int,const int) const;
};
but this is not acceptable C++
class Image // BAD CODE:
{
Grey getPixel(const int,const int) const;
RGB getPixel(const int,const int) const;
};
So back to your problem, in producing an array you want the pixel type to be completely runtime specified, maybe in the array half are grey/ half are RGB (e.g. adding two images together). This is polymorphism. Produce a base class Pixel and specialize down to the RGB/Grey types using public inheritence. Make the destructors virtual and any other polymorphic functions, e.g. get/set/display etc.
You might have this
class Pixel
{
public:
virtual ~Pixel();
virtual void display(Screen&) const =0; // abstract class
};
class RGB : public Pixel
{
public:
virtual ~RGB();
virtual void display(Screen&) const;
};
class Grey : public Pixel
{
public:
virtual ~Grey();
virtual void display(Screen&) const;
};
Obviously the classes need fleshing out and maybe a different architecture is good for your problem.
Then the only area for templates would then be in the Image class, you would have something like this
class Image
{
private:
Image** Array; .
public:
template
T** getImageMap() const;
template
T getPixelValue(const int,const int) const;
};[/code]
Image** Array may well be a better managed array boost::multi_array/ or a matrix class.
getImageMap would convert the Image storeage in Array into the required pixel form.
Great care MUST be taken about the getImageMap memory management (i.e who manages.) and a smart pointer of some sort or an managed class (boost::multi_array) is likely to be a good solution.
Likewize getPixelValue would return a pixel in the given form.
Note:: In the code useage you have to specify the template return type like this:
Image A;
// .. stuff
RGB p=A.getPixelValue<RGB>(4,5);
This does not work since the compiler is not allowed to look past the determination point (=) since a cast etc could be used.
// NOT c++
Image A;
// .. Stuff
RGB p=A.getPixelValue(4,5); // Ill defined
The key to these problems is always separation. Write the Image separately from the pixel part.
Hope that helps.