Thread: Deducing Function Template Arguments

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Deducing Function Template Arguments

    Hello everyone,


    Here is my understanding of what Bjarne said in his The C++ Programming Language book. I am not sure whether my understanding is correct? I am somewhat confused about the following statements.

    My understanding is,

    means if a function call like this,

    Code:
    void func(const A a) // A is a specific type
    then compiler can deduce argument type of template function below to A?

    Code:
    template <class T> func (T a)
    Bjarne's original statements are,

    --------------------
    C.13.4 Deducing Function Template Arguments

    A compiler can deduce a type template argument, T or TT , and a nontype
    template argument, I, from a template function argument with a type composed of the following constructs:

    Code:
    T const T volatile T
    T*  T& T[constant_expression]
    type[I]  class_template_name<T> class_template_name<I>
    T T<T > T<I > T<>
    --------------------


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    means if a function call like this,
    That's not a function call. That's a head. Add a semicolon, it's a prototype. Add braces, it's an implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry CornedBee,


    My bad. :-)

    If I change to the following in my original post, do you think my
    understanding is correct?

    Code:
    const A a; // A is a specific type
    func(a); // can deduce T to A.
    Quote Originally Posted by CornedBee View Post
    That's not a function call. That's a head. Add a semicolon, it's a prototype. Add braces, it's an implementation.

    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it can. So long as you don't pass unambiguous types, the compiler can deduce the type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Question answered.

    Quote Originally Posted by Elysia View Post
    Yes, it can. So long as you don't pass unambiguous types, the compiler can deduce the type.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM