By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,650 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,650 IT Pros & Developers. It's quick & easy.

template metaprogramming question

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
On Oct 12, 3:03*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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,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

P: n/a
no*********************@yahoo.com wrote:
On Oct 12, 3:03*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>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,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 discussion thread is closed

Replies have been disabled for this discussion.