template function argument deduce

Discussion in 'C Programming' started by George2, Mar 11, 2008.

  1. George2

    George2

    Joined:
    Feb 12, 2008
    Messages:
    108
    Likes Received:
    0
    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 (Text):

    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
     
    George2, Mar 11, 2008
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim West
    Replies:
    3
    Views:
    1,014
    Jim West
    Oct 7, 2004
  2. Dan Krantz
    Replies:
    3
    Views:
    582
    Howard Hinnant
    Jun 13, 2006
  3. gretean@comcast.net

    "could not deduce template argument" error

    gretean@comcast.net, Nov 10, 2006, in forum: C++
    Replies:
    7
    Views:
    742
    gretean@comcast.net
    Nov 10, 2006
  4. Ed
    Replies:
    1
    Views:
    520
    James Kanze
    Aug 14, 2008
  5. Maik
    Replies:
    2
    Views:
    1,189
  6. nguillot
    Replies:
    5
    Views:
    800
  7. WQ
    Replies:
    2
    Views:
    329
  8. Nephi Immortal
    Replies:
    0
    Views:
    348
    Nephi Immortal
    Jan 26, 2013
Loading...