Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template function argument deduce

Reply
Thread Tools

template function argument deduce

 
 
George2 George2 is offline
Senior Member
Join Date: Feb 2008
Posts: 108
 
      03-11-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to deduce template argument implicitly? Nephi Immortal C++ 0 01-26-2013 04:14 AM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
deduce element type of c-array template argument Maik C++ 2 12-03-2008 05:53 PM
Deduce function template argument Ed C++ 1 08-14-2008 08:13 PM
"could not deduce template argument" error gretean@comcast.net C++ 7 11-10-2006 03:03 PM



Advertisments