template non-type arguments

Discussion in 'C++' started by Hicham Mouline, Feb 27, 2008.

  1. hi,
    can a non-type argument of a template be of float type?
    If no, how can one get same intended result?
    rds,
    Hicham Mouline, Feb 27, 2008
    #1
    1. Advertising

  2. Hicham Mouline a écrit :
    > hi,
    > can a non-type argument of a template be of float type?


    no.

    > If no, how can one get same intended result?


    I don't know the result you want :)
    If you want to parametrize a type with a float value, then you cannot
    unless you wrap it in a parametrized class.

    template<int ID>
    struct ParamFloat;

    template<>
    struct ParamFloat<0>
    {
    static const float value=0.0;
    };

    template<>
    struct ParamFloat<1>
    {
    static const float value=3.14116;
    };

    //...

    Then

    struct<class FLOAT>
    struct mult2
    {
    static float value=FLOAT::value*2f;
    };


    If you want to use or compute a float value (computing series ...) then
    you just embed the value:

    template<unsigned N>
    struct slow_pi
    {
    static const double value= sqrt2<N-1>::value
    + ((N%2)?1.0:-1.0)/(2N+1);
    };

    template<>
    struct slow_pi<0>
    {
    static const double value =1;
    };

    Michael
    Michael DOUBEZ, Feb 27, 2008
    #2
    1. Advertising

  3. Hicham Mouline

    Jeff Schwab Guest

    Jeff Schwab, Feb 27, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IR
    Replies:
    3
    Views:
    412
  2. Replies:
    1
    Views:
    2,088
    Gianni Mariani
    Jun 8, 2007
  3. iglpdc
    Replies:
    3
    Views:
    319
    iglpdc
    Nov 20, 2007
  4. Replies:
    4
    Views:
    265
    Francesco
    Aug 27, 2008
  5. Replies:
    0
    Views:
    301
Loading...

Share This Page