Partial template specialization for function

V

vectorizor

Hello,

I've got a template function:

template <typename U, typename T>
void func(ColourImage<U> &input,GrayImage<T> &out_r, GrayImage<T>
&out_g, GrayImage<T> &out_b);

and I would like to specialize it for U = unsigned char. So I wrote

template <u8, typename T>
void func(ColourImage<u8> &input, GrayImage<T> &out_r, GrayImage<T>
&out_g, GrayImage<T> &out_b);

But the specialization never gets called. And I have no idea why.
Could someone explain me what I am doing wrong? The code is below, and
all the action happens at the very bottom.

Thanks

ALex




#include <windows.h>
#include <iostream>
#include <math.h>

typedef unsigned char u8;
typedef float f32;




////////////////////
///// MEMORY ROUTINES
////////////////////

enum { MemoryAlignment=64};

void* AllocateMemory(size_t size)
{
return _aligned_malloc(size, MemoryAlignment);
}

void ReleaseMemory(void *memblock)
{
return _aligned_free(memblock);
}

int ComputeAlignedWidth(int width)
{
int alignment_needed = MemoryAlignment / sizeof(float);
return (int)ceil((float)width/(float)alignment_needed) *
alignment_needed;
}




////////////////////
///// CLASS DECLARATION
////////////////////


template <typename T>
struct Image
{
public: // members
// std information
int width, height, depth;

// actual width of the buffer
// buffer holding image data is padded to be a multiple
// of MemoryAlignment for optimisation purposes
int width_padded;

// dimensions helper
int firstRow, lastRow, firstCol, lastCol;

// pointer to the image data
T* data;

public: // methods
// ctor
Image():
width(0),height(0),depth(0),
width_padded(0),
firstRow(0), lastRow(0), firstCol(0), lastCol(0),
data(NULL)
{
}

// dtor
~Image()
{
}

// memory management
void Allocate() { data =
static_cast<T*>(AllocateMemory(width_padded*height*depth*sizeof(T)));}
void Release () { ReleaseMemory(data);}

// pixel access
// virtual T& operator() (int row, int col)

// dimensions management
void SetDimensions(int h, int w, int d){
height = h;
width = w;
depth = d;
width_padded = ComputeAlignedWidth(width);
firstRow = 0;
firstCol = 0;
lastRow = height-1;
lastCol = width-1;
}

// size information
int GetTotalSize(bool padded=false){
if (padded) return width_padded*height*depth*sizeof(T);
else return width *height*depth*sizeof(T);
}
int GetImageSize(bool padded=false){
if (padded) return width_padded*height*depth;
else return width *height*depth;
}
int GetPlaneSize(bool padded=false){
if (padded) return width_padded*height;
else return width *height;
}

};




template <typename T>
struct GrayImage : public Image<T>
{
public: // methods
// ctor
GrayImage():
Image()
{
depth=1;
}

// pixel access
T& operator() (int row, int col)
{
return data[row*width_padded + col];
}
};


template <typename T>
struct ColourImage : public Image<T>
{
public: // methods
// ctor
ColourImage():
Image()
{
depth=3;
}

// pixel access
T& operator() (int row, int col, int channel)
{
return data[(row*width_padded + col)*3 + channel];
}
};


template <typename U, typename T>
void MapFrom_sRGB(ColourImage<U> &input,
GrayImage<T> &out_r, GrayImage<T> &out_g,
GrayImage<T> &out_b)
{
std::cout << "not partial" << std::endl;
}

template <u8, typename T>
void MapFrom_sRGB(ColourImage<u8> &input,
GrayImage<T> &out_r, GrayImage<T> &out_g,
GrayImage<T> &out_b)
{
cout << "partial" << endl;
}


int main(int argc, char* argv[])
{
ColourImage<u8> input;
GrayImage<f32> output1, output2, output3;

input.SetDimensions(2000, 2000, 3); input.Allocate();
output1.SetDimensions(input.height, input.width, 1);
output1.Allocate();
output2.SetDimensions(input.height, input.width, 1);
output2.Allocate();
output3.SetDimensions(input.height, input.width, 1);
output3.Allocate();

MapFrom_sRGB(input, output1, output2, output3);

input.Release(); output1.Release(); output2.Release();
output3.Release();

return 0;
}
 
R

Robert Bauck Hamar

vectorizor said:
Hello,

I've got a template function:

template <typename U, typename T>
void func(ColourImage<U> &input,GrayImage<T> &out_r, GrayImage<T>
&out_g, GrayImage<T> &out_b);

and I would like to specialize it for U = unsigned char. So I wrote

template <u8, typename T>
void func(ColourImage<u8> &input, GrayImage<T> &out_r, GrayImage<T>
&out_g, GrayImage<T> &out_b);


This creates another template. You can call it like this:
func<u8(42)>(ci, gi1, gi2, gi3);
for appropriate variables ci and gi*.

try:

template <class T>
void func(ColourImage said:
But the specialization never gets called. And I have no idea why.

A function template cannot be partially specialized (yet). You must create
another function template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top