473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Partial template specialization for function

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;
}

Jun 25 '07 #1
1 1616
vectorizor wrote:
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<u8>& input, GrayImage<T>& /* ... */);
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.

--
rbh
Jun 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... ...
10
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.