|
-
Oct 29th, 2001, 08:05 PM
#1
Thread Starter
PowerPoster
what is the difference between...
template <class X> and
template <typename X>
????
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Oct 29th, 2001, 09:52 PM
#2
For example?
Code:
template<int x>
and
template<class t>
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 <class t>
struct someStruct
{
t x;
t y;
};
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)).
Z.
-
Oct 30th, 2001, 04:55 AM
#3
transcendental analytic
Zaei, can you post the partial evaluation for fibonacci series using templates again? the search doesn't work :/
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 07:02 AM
#4
Here ya go kedaman:
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 };};
Z.
-
Oct 30th, 2001, 07:32 AM
#5
transcendental analytic
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/
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 08:08 AM
#6
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 30th, 2001, 08:20 AM
#7
transcendental analytic
It sure is cool, but it would be even cooler if this did work in MSVC
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 08:44 AM
#8
try it this way kedaman:
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 }; };
Not tested, just a quick rewrite of the code below... Try it out, and let me know what happens =).
Z.
-
Oct 30th, 2001, 08:50 AM
#9
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.
-
Oct 30th, 2001, 09:13 AM
#10
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 11:17 AM
#11
transcendental analytic
Solutions
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?!?
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;
};
IT would be nicer to have any type of constants than enumerated short integers.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 02:26 PM
#12
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.
-
Oct 30th, 2001, 02:43 PM
#13
transcendental analytic
Can I have say complex value in an enumeration?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 02:54 PM
#14
-
Oct 30th, 2001, 03:24 PM
#15
transcendental analytic
something like
enum { x=cmplx(y,z) }
which would make x a complex value (a struct with 2 floats)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 04:03 PM
#16
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.
-
Oct 30th, 2001, 04:48 PM
#17
Thread Starter
PowerPoster
cool, thanks for the replies guys. heh, thanks for the fibonacci code too
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|