template metaprogramming question

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

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!
 
K

Kai-Uwe Bux

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
 
N

nooneinparticular314159

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!
 
K

Kai-Uwe Bux

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
 
N

nooneinparticular314159

(e-mail address removed) 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!
 
K

Kai-Uwe Bux

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top