template <class X> and
template <typename X>
????
Printable View
template <class X> and
template <typename X>
????
For example?
The first one <int x> would take in a number (or char, it it were <char x>, etc), while <class t> takes in a class type, int, char, etc. So, say you have a template struct:Code:template<int x>
and
template<class t>
You can use t in place of a type name. You can use the <int x> for things like template metaprogramming (using the compiler to find values at compile time, instead of at run time (fibinocci (sp?) numbers are a good example)).Code:template <class t>
struct someStruct
{
t x;
t y;
};
Z.
Zaei, can you post the partial evaluation for fibonacci series using templates again? the search doesn't work :/
Here ya go kedaman:
Z.Code:template <unsigned int x>
struct fib
{
enum
{
fib = fib<x-1>::fib + fib::<x-2>::fib
};
};
template <> struct fib<0> { enum { fib = 0 };};
template <> struct fib<1> { enum { fib = 1 };};
doh! I wanted a template with two parameters, but i forgot that you only need one for fibonacci.
Thanks anyways.
here's an integer power template that doesn't work in MSVC(because of the other parameter), and I want to know if anyone has a similar that will work for MSVC
http://www.extreme.indiana.edu/~tveldhui/papers/pepm99/
This is cool. First I thought this must be impossible because the template parameter must be a constant. But then I realized that constant - constant IS a constant. Really cool.
It sure is cool, but it would be even cooler if this did work in MSVC
http://www.extreme.indiana.edu/~tvel...epm99/img4.gif
try it this way kedaman:
Not tested, just a quick rewrite of the code below... Try it out, and let me know what happens =).Code:template <int x, int y>
struct pow
{
enum
{
pow = x* pow<x, y-1>::pow
};
};
template <int x, 0> struct pow { enum { pow = x }; };
Z.
uhh, should have been "code above" =).
Anyway, doesnt work, I just tested it out. Maybe in VC.NET ::shrug::
Funny thing is, I was thinking about a pow template earlier =).
Z.
You can make a power of 2 template but not a power of n :/
So much for template specification when you have to specify all parameters :(
Ok, I got around it nicely by forcefully resetting x parameter so that they will both line up at x,1 to 0,0.
But, it doesn't seem to work using constants, why?!?
IT would be nicer to have any type of constants than enumerated short integers.PHP Code:template <int x, int y>
struct pwr2{
static const int p;};
template <int x, int y> const int pwr2<x,y>:: p= x*pwr2<x*(y>1),y-1>:: p;
template <> const int pwr2<0,0>:: p=1;
template <int x, int y>
struct pwr{enum{
p = x* pwr<x*(y>1), y-1>:: p
};};
template <>
struct pwr<0,0>{enum{
p = 1
};};
int main(){
cout << pwr<2,3>:: p << endl;
cout << pwr2<2,3>:: p << endl;
return 0;
};
Enumerations compile to the smallest possible data type for the number of parameters inside of them. For almost all enumerations that I create, I have a "FORCE_DWORD = 0x79999999" value to force the enumeration to compile to 32 bits, so it shouldnt be a problem to have large enums in your templates.
Z.
Can I have say complex value in an enumeration?
Eh? Explain?
Z.
something like
enum { x=cmplx(y,z) }
which would make x a complex value (a struct with 2 floats)
I have no idea. You might try to cast the cmplx() result into an __int64, and assign to the enum, and then get the values out using a union, etc.
Z.
cool, thanks for the replies guys. heh, thanks for the fibonacci code too ;)