Results 1 to 17 of 17

Thread: what is the difference between...

  1. #1

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340

    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

  2. #2
    Zaei
    Guest
    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.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  4. #4
    Zaei
    Guest
    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.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  8. #8
    Zaei
    Guest
    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.

  9. #9
    Zaei
    Guest
    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.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Talking 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 xint y>
    struct pwr2{
        static const 
    int p;};
    template <int xint y>    const int pwr2<x,y>:: px*pwr2<x*(y>1),y-1>:: p;
    template <>                const int pwr2<0,0>:: p=1;

    template <int xint y>
    struct pwr{enum{      
        
    xpwr<x*(y>1), y-1>:: p  
    };};
    template <> 
    struct pwr<0,0>{enum
        

    };};

    int main(){ 
        
    cout << pwr<2,3>:: << endl;
        
    cout << pwr2<2,3>:: << 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.

  12. #12
    Zaei
    Guest
    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.

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  14. #14
    Zaei
    Guest
    Eh? Explain?

    Z.

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  16. #16
    Zaei
    Guest
    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.

  17. #17

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    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
  •  



Click Here to Expand Forum to Full Width