473,769 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template metaprogramming question

I'm trying to do a template with specialization. I declare two
templates, one of which looks like:

template<int a, int b>
struct MOO
{

enum{
VALUE = b == 0 ? a: MOO<some stuff>::VALUE
};
};

template<>
struct MOO<int a, 0>
{
enum{VALUE = a};
};

The problem is, I get some compiler errors that don't make sense after
the second struct declaration:

1) Error: missing '>' to terminate the template argument list (where
is it missing from?!)
2) Error: wrong number of template arguments (1, should be 2). <---I
see two there. What am I missing?
3) Invalid type in declaration befre ',' token.

For 3, if I try declaring it as two numbers, say 0,0, it doesn't
complain about that. But I am trying to call the specialization with
an integer argument and a constant (0). What am I doing wrong?

Thanks!
Oct 12 '08 #1
3 3133
no************* ********@yahoo. com wrote:
I'm trying to do a template with specialization. I declare two
templates, one of which looks like:

template<int a, int b>
struct MOO
{

enum{
VALUE = b == 0 ? a: MOO<some stuff>::VALUE
};
};

template<>
struct MOO<int a, 0>
{
enum{VALUE = a};
};
[snip]

Try:

template<int a>
struct MOO<a, 0>
{
enum{VALUE = a};
};
Best

Kai-Uwe Bux
Oct 12 '08 #2
On Oct 12, 3:03*am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
nooneinparticul ar314...@yahoo. com wrote:
b) Why "If"? You should know, you have the code.
It did work perfectly. Thank you. :-) That was the problem with my
syntax.

Huh? What do you mean by "same set of arguments"?

a) The number: two arguments are passed.
b) The values: you can only call MOO<a,bfrom MOO<a,b>.

(a) is true. (b) is false.
What I mean is, the template for the recursive case takes two
arguments, and must have those, because it is operating on two
integers. So that's ok so far. But then for the termination step,
the template has only one argument even though the struct has two
arguments (one of which I supply.) But when Moo calls itself, it is
calling itself with two arguments. ie:

Moo calls Moo(a,b), and Moo requires two arguments so each time Moo is
called, I would think that the compiler would be checking for a
template that either takes two arguments (the two integers), or for a
template<which indicates specialization. But neither of these is
true for the termination step. The termination template has *one*
argument (although the struct has two, one supplied by me as a
constant). So I would think that that would never match the call to
Moo, and that is where I am getting confused. How can this work with
just one argument when the template is always called with two?

Thanks!
Oct 12 '08 #3
no************* ********@yahoo. com wrote:
On Oct 12, 3:03*am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>nooneinparticu lar314...@yahoo .com wrote:
>Huh? What do you mean by "same set of arguments"?

a) The number: two arguments are passed.
b) The values: you can only call MOO<a,bfrom MOO<a,b>.

(a) is true. (b) is false.

What I mean is, the template for the recursive case takes two
arguments, and must have those, because it is operating on two
integers. So that's ok so far. But then for the termination step,
the template has only one argument even though the struct has two
arguments (one of which I supply.) But when Moo calls itself, it is
calling itself with two arguments. ie:

Moo calls Moo(a,b), and Moo requires two arguments so each time Moo is
called, I would think that the compiler would be checking for a
template that either takes two arguments (the two integers), or for a
template<which indicates specialization.
You are forgetting about partial specializations .
But neither of these is
true for the termination step. The termination template has *one*
argument (although the struct has two, one supplied by me as a
constant). So I would think that that would never match the call to
Moo, and that is where I am getting confused. How can this work with
just one argument when the template is always called with two?
The partial specialization of MOO that marks the end of the recursion has
two parameters. It just happens that one of them is 0. That is why it is a
partial specialization: it only fixes some of the template parameters.
Best

Kai-Uwe Bux
Oct 12 '08 #4

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

Similar topics

12
2162
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 code)? It's not a perfect analogy, but it seems to be a reasonable statement...
5
1724
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!
21
2105
by: Protoman | last post by:
I've been looking at template metaprogramming. It seems really cool, make the compiler do most of the work. I have very simple program that uses TMP,it calculates the square of a number, but it doesn't seem to work. Here it is: #include <iostream> #include <cstdlib> using namespace std; template<int n>
2
1661
by: Ganesh | last post by:
I am not sure if the question is relevant to this group. Is there a proof that template metaprogramming is Turing-complete? Is the proof trivial - just replace all the template instantiaion with a function call and since function calls are turing complete, template metaprogramming is turing complete. I am not sure if this is a valid proof. I may not even make sense, as I am not very confident when it comes to theoritical computer...
7
3544
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 metaprogramming is supported in C# (.NET)? For reference I found it: http://en.wikipedia.org/wiki/Template_metaprogramming. Thanks to all.
1
2389
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in producing high performance C++ code; code that is provably correct. The issue here is orthogonal to the question of expression templates, which at present seem to me to be relatively simple.
5
3621
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 anyone explain why loop unrolling doesn't play well with templates? Or better, can someone submit a code fragment to get desired results? Here are the command-lines I use to generate code: "g++ -DTEMPLATE=0 -o gotofun0 gotofun.cpp" works exactly as...
4
1868
by: suman.nandan | last post by:
Hi C++ Experts ! I have a little weird requirement. I have a base class, say B and lots of classes D1 .. Dn publicly derived from it. Over the course of development the number of derived classes may increase. I want that before the execution of one of my particular code, I should have a list of pointers to all the derived classes, say std::list<B*ptrList; // contains 'n' elements which are pointers to D1 .. Dn.
3
1854
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 Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather I want to learn heavyweight templates and this is the only
12
3368
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
9586
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.