template metaprogramming question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nooneinparticular314159@yahoo.com

    #1

    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!
  • Kai-Uwe Bux

    #2
    Re: template metaprogramming question

    nooneinparticul ar314159@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

    Comment

    • nooneinparticular314159@yahoo.com

      #3
      Re: template metaprogramming question

      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!

      Comment

      • Kai-Uwe Bux

        #4
        Re: template metaprogramming question

        nooneinparticul ar314159@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

        Comment

        Working...