template function argument deduce

This is a discussion on template function argument deduce within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Why in the following code segment (along with Bjarne's comments), function get_new can not deduce template parameter type ...

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

    template function argument deduce

    Hello everyone,


    Why in the following code segment (along with Bjarne's comments), function get_new can not deduce template parameter type to be int by its return value assignment to int * p1, and deduce T* is int*, and so T is int (e.g. as we did for function release in the same sample below)?

    Code:
    class Memory { // some Allocator
    public:
    template <class T> T * get _new ();
    template <class T > void release (T&);
    / / ...
    };
    
    template <class Allocator> void f (Allocator &m )
    {
    int * p1 = m.get _ new <int>(); // syntax error: int after lessthan
    operator
    int * p2 = m.template get _ new <int>(); // explicit qualification
    / / ...
    m.release (p1); // template argument deduced: no explicit qualification needed
    m.release (p2 );
    }
    Explicit qualification of get _new() is necessary because its template parameter cannot be deduced. In this case, the t e m p l a t e prefix must be used to inform the compiler (and the human reader) that get _new is a member template so that explicit qualification with the desired type of element is possible. Without the qualification with template , we would get a syntax error because the < would be assumed to be a less than operator. The need for qualification with template is rare because most template parameters are deduced.


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Template parameters can only be deduced from function parameters, never from return types. That's just the way it is.

    And what's with all the stray spaces?
    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
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    Template parameters can only be deduced from function parameters, never from return types. That's just the way it is.

    And what's with all the stray spaces?
    Question answered.


    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 10:36 PM
  2. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 06:06 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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