Clarification of Function Templates

This is a discussion on Clarification of Function Templates within the C++ Programming forums, part of the General Programming Boards category; I'm coming from C and I'm picking up C++ pretty fast. I just have a small question about function templates. ...

  1. #1
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Clarification of Function Templates

    I'm coming from C and I'm picking up C++ pretty fast. I just have a small question about function templates. In my book it gives the following function template:

    Code:
    template < class T >
    T maximum(T value1, T value2, T value3)
    {
       T max = value1;
    
       if (value2 > max)
          max = value2;
    
    etc.. etc..
    My question deals with the head of the template. The book says that inside the brackets the formal type parameters are preceded either by typename or class (as it is in the example).

    My question is does it matter which? And if it does matter, when do I use one or the other?

    thanks

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Stroustroup prefers class ("it's shorter"), typename and class are both fine by the standard, although class is pretty much the only thing used in practice, and I've even heard stories of some compiler's not recognizing the typename syntax.

    However, the arguments themselves don't need to be classes are typenames, they could be, say, constant integers at compile time, but they are usually classes.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. I need help with templates!
    By advocation in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2005, 08:27 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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