![]() |
| | #1 |
| Registered User Join Date: Jan 2007
Posts: 223
| template to let compiler generate zero 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;
};
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
}
|
| KIBO is offline | |
| | #2 | |
| The larch Join Date: May 2006
Posts: 3,222
| Have you tried: Code: T Total = std::accumulate(Chances, Chances + Size, T());
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #3 |
| Registered User Join Date: Jan 2007
Posts: 223
| well no does Code: double x = double() it seems to work with VS2005 but im not sure if its standard? |
| KIBO is offline | |
| | #4 | |
| The larch Join Date: May 2006
Posts: 3,222
| 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. Quote:
| |
| anon is offline | |
| | #5 |
| Registered User Join Date: Jan 2007
Posts: 223
| didnt know that. thanks! no need for overcomplicated template stuff then |
| KIBO is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Template Recursion Pickle | SevenThunders | C++ Programming | 20 | 02-05-2009 09:45 PM |
| matrix class | shuo | C++ Programming | 2 | 07-13-2007 01:03 AM |
| OpenScript2.0 Compiler | jverkoey | C++ Programming | 3 | 10-30-2003 01:52 PM |
| templates with pointers | Cipher | C++ Programming | 3 | 11-18-2002 11:45 AM |
| template and compiler ?!! | Unregistered | C++ Programming | 2 | 06-28-2002 03:47 AM |