C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-15-2009, 01:08 AM   #1
Registered User
 
Join Date: Jan 2007
Posts: 223
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.
KIBO is offline   Reply With Quote
Old 06-15-2009, 01:18 AM   #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:
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).
anon is offline   Reply With Quote
Old 06-15-2009, 02:43 AM   #3
Registered User
 
Join Date: Jan 2007
Posts: 223
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?
KIBO is offline   Reply With Quote
Old 06-15-2009, 03:10 AM   #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:
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).
anon is offline   Reply With Quote
Old 06-15-2009, 03:51 AM   #5
Registered User
 
Join Date: Jan 2007
Posts: 223
didnt know that. thanks!

no need for overcomplicated template stuff then
KIBO is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22