473,386 Members | 1,764 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,386 software developers and data experts.

template metaprogramming (unroll loop)

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 ?

--
pierre

Jul 6 '07 #1
1 2916
On Jul 6, 11:17 am, ticapix <pierre.gronl...@gmail.comwrote:
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 lengthstruct 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 idxstruct 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;

}
Jul 6 '07 #2

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

Similar topics

12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
5
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
2
by: spasmous | last post by:
I just found out MSVC++ doesn't unroll loops on any optimization setting. So I manually unrolled the inner loop of a slow part of my program... The result was a 2X gain in speed. So I'm wondering...
2
by: Martin Gernhard | last post by:
Hi, I'm trying to use expression templates to calculate values at compile time. Doing it with just one parameter is no problem: ///Begin Listing 1 #include <iostream> using namespace std; ...
8
by: Jon Rea | last post by:
http://osl.iu.edu/~tveldhui/papers/Template-Metaprograms/meta-art.html Can anyone shed some light on the need for stuff like this when using any of the most modern compilers? If i have a...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.