template metaprogramming (unroll loop)

T

ticapix

I wrote this little code which doesn't compile:
"main.cpp:46: erreur: function template partial specialization"

//// ------------------ BEGIN ------------------ ////

#include <iostream>

struct kernel {
int length;
int coords[9][2];

};

static const kernel g_kernels[2] = {
{2, {{-1, 0}, {1, 0}}},
{2, {{0, -1}, {0, 1}}},

};

template<int idx, int length>
/*inline*/ char ApplyKernel(unsigned char* buff) {
std::cout << g_kernels[idx].coords[length-1][1] << ", " <<
g_kernels[idx].coords[length-1][0] << std::endl;
return ApplyKernel<idx, length-1>(buff);

}

template<int idx>
/*inline*/ char ApplyKernel<idx, 0>(unsigned char* buff) {
std::cout << g_kernels[idx].coords[0][1] << ", " <<
g_kernels[idx].coords[0][0] << std::endl;
return 0;

}

typedef char (apply_t)(unsigned char* buff);

apply_t *fcts[] = {
ApplyKernel<0, 2-1>,
ApplyKernel<1, 2-1>

};

static unsigned char buff[999];

int main(void) {
fcts[0]((unsigned char*)buff);
fcts[1]((unsigned char*)buff);
return 0;

}

//// ------------------ END ------------------ ////

I'm trying to unroll the loop for, but I can figure out how to
specialize a "stop" template method like:

template<int idx>
char ApplyKernel<idx, 0>(unsigned char* buff) {...};

How can I bypass this restriction ?
 
N

Neelesh Bodas

I wrote this little code which doesn't compile:
"main.cpp:46: erreur: function template partial specialization"

//// ------------------ BEGIN ------------------ //// [code snippet chopped

template<int idx>
/*inline*/ char ApplyKernel<idx, 0>(unsigned char* buff) {
std::cout << g_kernels[idx].coords[0][1] << ", " <<
g_kernels[idx].coords[0][0] << std::endl;
return 0;

According to std C++, partial specialization of function templates is
not allowed.
One alternative is to make the function ApplyKernel static inside a
class and do then do a partial template specialization of the class.


//// ------------------ BEGIN ------------------ ////

#include <iostream>

struct kernel {
int length;
int coords[9][2];

};

static const kernel g_kernels[2] = {
{2, {{-1, 0}, {1, 0}}},
{2, {{0, -1}, {0, 1}}},

};


template<int idx, int length> struct Wrapper
{
/*inline*/ static char ApplyKernel(unsigned char* buff) {
std::cout << g_kernels[idx].coords[length-1][1] << ", " <<
g_kernels[idx].coords[length-1][0] << std::endl;
return Wrapper<idx, length-1>::ApplyKernel(buff);
}
};

template<int idx> struct Wrapper<idx,0> {
/*inline*/ static char ApplyKernel(unsigned char* buff) {
std::cout << g_kernels[idx].coords[0][1] << ", " <<
g_kernels[idx].coords[0][0] << std::endl;
return 0;

}
};

typedef char (apply_t)(unsigned char* buff);

apply_t *fcts[] = {
Wrapper<0, 2-1>::ApplyKernel,
Wrapper<1, 2-1>::ApplyKernel

};

static unsigned char buff[999];

int main(void) {
fcts[0]((unsigned char*)buff);
fcts[1]((unsigned char*)buff);
return 0;

}
 

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,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top