int template argument

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hicham Mouline

    int template argument

    hi,

    I have a template function <typename cT, typename tT, int n>
    {
    .....
    double array[n];

    if (n==2)
    binomial code
    else if (n==3)
    trinomial code (array[0] array[1] array[2])
    else
    .....


    compiler tries to compile the "trinomial code" code even when the template
    is instantiated with n=2,
    and that gives warnings because of array[2]

    Is there a way to make the compiler ignore the non-binomial code ?
    some way with boost perhaps?

    rds,


  • Christian Hackl

    #2
    Re: int template argument

    Hicham Mouline wrote:
    I have a template function <typename cT, typename tT, int n>
    {
    ....
    double array[n];
    >
    if (n==2)
    binomial code
    else if (n==3)
    trinomial code (array[0] array[1] array[2])
    else
    ....
    >
    >
    compiler tries to compile the "trinomial code" code even when the template
    is instantiated with n=2,
    and that gives warnings because of array[2]
    >
    Is there a way to make the compiler ignore the non-binomial code ?
    some way with boost perhaps?
    There's boost::array, which you could instead of the naked array.





    --
    Christian Hackl

    Comment

    • jason.cipriani@gmail.com

      #3
      Re: int template argument

      "Christian Hackl" <hacki@sbox.tug raz.atwrote in message news:frp17l
      $kfv$1@aioe.org ...
      Hicham Mouline wrote:
      >
      >I have a template function <typename cT, typename tT, int n>
      >{
      >....
      >double array[n];
      >>
      >if (n==2)
      > binomial code
      >else if (n==3)
      > trinomial code (array[0] array[1] array[2])
      >else
      >....
      >>
      >>
      >compiler tries to compile the "trinomial code" code even when the template
      >is instantiated with n=2,
      >and that gives warnings because of array[2]
      >>
      >Is there a way to make the compiler ignore the non-binomial code ?
      >some way with boost perhaps?
      >
      There's boost::array, which you could instead of the naked array.
      >
      http://www.boost.org/doc/html/array.html
      If the boost array is just a wrapper for a constant-sized array,
      wouldn't a compiler that generates warnings about out-of-bounds
      accesses complain when compiling the boost array template code, too?
      Or is it just hidden behind a function parameter, in which case the
      following would also kill the compiler warning in the original code:

      double & access_array (double *a, int n) {
      return a[n];
      }

      And then instead if:

      ....
      else if (n==3) {
      array[0] ...
      array[1] ...
      array[2] ...
      }
      ....

      It becomes:

      ....
      else if (n==3) {
      access_array(ar ray, 0) ...
      access_array(ar ray, 1) ...
      access_array(ar ray, 2) ...
      }
      ....

      Jason

      Comment

      • Kai-Uwe Bux

        #4
        Re: int template argument

        Hicham Mouline wrote:
        hi,
        >
        I have a template function <typename cT, typename tT, int n>
        {
        ....
        double array[n];
        >
        if (n==2)
        binomial code
        else if (n==3)
        trinomial code (array[0] array[1] array[2])
        else
        ....
        You should overload the function for different values of n instead of using
        an if statement to distinguish the cases. Alternatively, you can isolate
        that piece of code into another function and overload that. Something like:

        template < int n, typename cT, typename tT >
        void polynomial_code ( ct & c, tT & t );

        template < typename cT, typename tT>
        void polynomial_code <2,cT,tT( ct & c, tT & t ) {
        binomial code
        }

        template < typename cT, typename tT>
        void polynomial_code <3,cT,tT( ct & c, tT & t ) {
        trinomial code
        }


        template <typename cT, typename tT, int n>
        ...
        polynomial_code <n>( ct_arg, tt_arg );
        ...

        Maybe, I got the syntax slightly wrong, but I hope the idea is clear.


        [snip]

        Best

        Kai-Uwe Bux

        Comment

        • Greg Herlihy

          #5
          Re: int template argument

          On Mar 18, 10:26 am, "Hicham Mouline" <hic...@mouline .orgwrote:
          I have a template function <typename cT, typename tT, int n>
          {
          ....
          double array[n];
          >
          if (n==2)
            binomial code
          else if (n==3)
            trinomial code  (array[0] array[1] array[2])
          else
          Have you tried:

          double array[n];

          if (n==2)
          binomial code
          else if (n==3)
          trinomial code (array[0] array[1] array[n-1])

          Greg



          Comment

          Working...