473,422 Members | 2,235 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,422 software developers and data experts.

expression template, two arguments, partial specialisation error

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;

template <int n> void UNROLL(){
cout << "n = " << n << endl;
UNROLL<n-1>();
}

template <> void UNROLL<0>() {
cout << "Hit Zero!" << endl;
}

int main(){
UNROLL<10>();
return 0;
}
///Begin Listing 1

However, if I want to use two parameters, I have to explicitly set both in
the abortion rule like this:

///Begin Listing 2
#include <iostream>

using namespace std;

template <int n, int m> void UNROLL(){
cout << "m*n = " << m*n << endl;
UNROLL<n, m-1>();
}

template <> void UNROLL<5, 0>() {
cout << "Hit Zero!" << endl;
}

int main(){
UNROLL<5, 10>();
return 0;
}
//End Listing 2

Of course, this is not what I want, I want the recursion to abort with
n == (any value) and m == 0. I tried to change the second program like this:

template <int n> void UNROLL<n, 0>() {
cout << "Hit Zero!" << endl;
}

But this gives me the error

temprektest_func.cpp:12: error: partial specialization `UNROLL<n, 0>' of
function template

and more errors about exceeding the maximum instantiation depth of
templates.

I tried google first. I did not find anything exactly like what I wanted,
but guessing from examples involving classes, this seemed to be the syntax
to use. Is this a limitation of my compiler (G++ 3.3.6), am I doing it
wrong or is it simply not possible to specialize expression templates in
this way?

Thanks, Martin

Nov 22 '05 #1
2 1717
Martin Gernhard wrote:
I'm trying to use expression templates to calculate values at compile time.
[...]
template <int n, int m> void UNROLL(){
cout << "m*n = " << m*n << endl;
UNROLL<n, m-1>();
}

template <> void UNROLL<5, 0>() {
cout << "Hit Zero!" << endl;
}

int main(){
UNROLL<5, 10>();
return 0;
}
//End Listing 2

Of course, this is not what I want, I want the recursion to abort with
n == (any value) and m == 0. I tried to change the second program like this:

template <int n> void UNROLL<n, 0>() {
cout << "Hit Zero!" << endl;
}

But this gives me the error

temprektest_func.cpp:12: error: partial specialization `UNROLL<n, 0>' of
function template

[...]


First of all, there are no partial specialisations of function templates.
Period. Once you admit that to yourself, you can start working towards
a solution, like making your 'UNROLL<n,m>' into a class template.

V
Nov 22 '05 #2
Victor Bazarov wrote:
Martin Gernhard wrote:
I'm trying to use expression templates to calculate values at compile
time.
[...]
template <int n, int m> void UNROLL(){
cout << "m*n = " << m*n << endl;
UNROLL<n, m-1>();
}

[...]
template <int n> void UNROLL<n, 0>() {
cout << "Hit Zero!" << endl;
}

But this gives me the error

temprektest_func.cpp:12: error: partial specialization `UNROLL<n, 0>' of
function template

[...]
First of all, there are no partial specialisations of function templates.
Period. Once you admit that to yourself, you can start working towards
a solution, like making your 'UNROLL<n,m>' into a class template.


Thank you very much, this was exactly the clarification I needed. Using the
last example from http://medialab.di.unipi.it/web/AP/MetaProgramming.ppt as
a rough guideline (I found it before my first posting but did not realize I
_needed_ to use classes) the program now calculates binomial coefficients.

To anyone interested, here it is. Because it is ignorant of integer
overflow, its usefulness is probably rather limited.

Thank you very much!

//Listing 1

#include <iostream>

using namespace std;

template <int k> int faculty(){
return k * faculty<k-1>();
}

template<> int faculty<0>(){
return 1;
}

// BinoCoeff - Calculates (n choose k)
template <int n, int k> class BinoCoeff{
public:
static void BinoCoeff::doIt(){
cout << "( " << n << " choose " << k << " ) = "
<< (faculty<n>()/(faculty<k>() * faculty<n-k>())) << endl;
}
};
// BinoCoeffs - Calculates all binomial coefficients
// (n choose k) with k = 0...n.
template <int n, int k, int i> class BinoCoeffs{
public:
static void next(){
BinoCoeff<n, k>::doIt();
BinoCoeffs<n, k+1, i-1>::next();
}
};

template <int n, int k> class BinoCoeffs<n, k, -1>{
public:
static void next(){
}
};
// AllBinoCoeffs - Calculates all binomial coefficients
// of all numbers from n to i.
template <int n, int i> class AllBinoCoeffs{
public:
static void next(){
BinoCoeffs<n, 0, n>::next();
AllBinoCoeffs<n+1, i-1>::next();
}
};

template <int n> class AllBinoCoeffs<n, -1>{
public:
static void next(){
}
};
typedef AllBinoCoeffs<0, 12> BinoCoeffs_0_to_12;

int main(){
BinoCoeffs_0_to_12 *binos = new BinoCoeffs_0_to_12;
binos->next();
delete binos;
}

Nov 22 '05 #3

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...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
1
by: Martin | last post by:
I'm trying to make a partial specialization of a class of mine. Can someone please tell me what's wrong with the following code? GCC gives me the error "invalid use of undefined type "class X<int,...
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.