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



LinkBack URL
About LinkBacks


