[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

template metaprogramming question

nooneinparticular314159@yahoo.com

10/12/2008 3:42:00 AM

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!
5 Answers

Kai-Uwe Bux

10/12/2008 3:51:00 AM

0

nooneinparticular314159@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

nooneinparticular314159@yahoo.com

10/12/2008 9:53:00 AM

0

If that works, I'm very confused about something. The struct MOO<int
a, 0> portion is supposed to be the base case for a recursive loop.
But that means that in order to be called, it must have two arguments,
since the recursive step in moo:

VALUE = b == 0 ? a: MOO<some stuff>::VALUE

must call moo with the same set of arguments, since it doesn't know in
advance that it has hit the base case. Am I not understanding this
correctly?

Thanks!

Kai-Uwe Bux

10/12/2008 10:04:00 AM

0

nooneinparticular314159@yahoo.com wrote:

> If that works,

a) Obviously, there is something upthread that you are responding to. It
would be very helpful if you quoted that.

b) Why "If"? You should know, you have the code.

> I'm very confused about something. The struct MOO<int
> a, 0> portion is supposed to be the base case for a recursive loop.
> But that means that in order to be called, it must have two arguments,
> since the recursive step in moo:
>
> VALUE = b == 0 ? a: MOO<some stuff>::VALUE
>
> must call moo with the same set of arguments, since it doesn't know in
> advance that it has hit the base case. Am I not understanding this
> correctly?

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,b> from MOO<a,b>.

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

Whether the recursion works out nicely or runs into an infinite loop depend
of what "some stuff" is. Unfortunately, my crystall ball is broken; so I
cannot help with parts of the code that I do not see.


Best

Kai-Uwe Bux

nooneinparticular314159@yahoo.com

10/12/2008 4:57:00 PM

0

On Oct 12, 3:03 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> nooneinparticular314...@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,b> from 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!

Kai-Uwe Bux

10/12/2008 7:45:00 PM

0

nooneinparticular314159@yahoo.com wrote:

> On Oct 12, 3:03 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
>> nooneinparticular314...@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,b> from 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