Thread: template to let compiler generate zero

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    template to let compiler generate zero

    How could I create a template to let the compiler generate a zero for all types?

    My guess was the following but I cant get it to compile

    Code:
    template<typename T>
    struct Zero
    {
      static T Value;
    };
    
    template<>
    struct Zero<float>
    {
      static float Value;
    };
    It would be handy for algorithms like accumulate which often deduce the template parameter from the starting zero in the argument list.

    Code:
    template<typename T>
    T Func(T *Chances, size_t Size)
    {
      T Total = std::accumulate(Chances, Chances + Size, 0);
    
      // do other stuff
    }

    The '0' in accumulate makes the compiler generate an accumulate<int>, if it was 0.0 it would be double.

    So what Id like is to be able to type:

    Code:
    template<typename T>
    T Func(T *Chances, size_t Size)
    {
      T Total = std::accumulate(Chances, Chances + Size, Zero<T>::Value);
    
      // do other stuff
    }
    so I can call Func with any type and the compiler chooses from the arguments from Func itself.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you tried:

    Code:
    T Total = std::accumulate(Chances, Chances + Size, T());
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    well no
    does

    Code:
    double x = double()
    create an x with 0.0 then?

    it seems to work with VS2005 but im not sure if its standard?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is the way to obtain the default value for a type. For example, this is how a std::vector is filled with all zero's when you create a vector<int> with a size parameter.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    didnt know that. thanks!

    no need for overcomplicated template stuff then

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 01:52 PM
  4. templates with pointers
    By Cipher in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 11:45 AM
  5. template and compiler ?!!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2002, 03:47 AM