Thread: Explain: A template function is not a function

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Gurgaon, India

    Explain: A template function is not a function

    template function is not a function. The prototype for a template function is
    maximum<T>(T, T). The function that this template creates when T is int is
    the function (not template function) maximum(int, int).
    I picked up this from a book. It says
    The main() function implicitly causes C++ to create two versions of maximum()
    How does C++ create the versions of the template maximum() ?
    Can you give me an example ?

    Creating a function from a template is called instantiating the template.
    Secondly, C++ does itself create a function from a template or do we have to do it somehow ?
    Last edited by AnishaKaul; 05-13-2010 at 06:18 AM.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    RING 0
    A template function isn't "created" until compile time when type parameters can be bound to it. In your case with the maximum function say you called one maximum with 2 doubles, and another with 2 floats, and perhaps even another with 2 ints. For each invocation seperate code for each type will have to be generated (3 unique functions).

  3. #3
    The larch
    Join Date
    May 2006
    Isn't it technically called a "function template", that is, a template for generating functions as requested?
    I might be wrong.

    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Deducing Function Template Arguments
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2008, 07:29 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM