Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it legal to assign a template function to a function pointer this way

Reply
Thread Tools

Is it legal to assign a template function to a function pointer this way

 
 
cindypwl@lycos.com
Guest
Posts: n/a
 
      03-19-2006
I was looking at some code in the MS NG, and I saw this code listed.
template <class T>class List{};

template<class T>
bool MySortFunc(List<T>& list){return true;}

template <class T>struct SORT{typedef bool (*FnPtr)(List<T>& list);};

int main(){
List<int> MyList;
List<long> MyList2;
SORT<int>::FnPtr Sort = &MySortFunc; //***No template type!!!!!!!
Sort(MyList);

I would think that the function pointer assignment would not compile
with out using the templae type.
SORT<int>::FnPtr Sort = &MySortFunc<int>;

But I was able to compile this with out the type on both VC++ and
Comeau.

So is this code legal, or are the compilers wrong to compile it
successfully?

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      03-19-2006
On 19 Mar 2006 05:00:16 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>I was looking at some code in the MS NG, and I saw this code listed.
>template <class T>class List{};
>
>template<class T>
>bool MySortFunc(List<T>& list){return true;}
>
>template <class T>struct SORT{typedef bool (*FnPtr)(List<T>& list);};
>
>int main(){
> List<int> MyList;
> List<long> MyList2;
> SORT<int>::FnPtr Sort = &MySortFunc; //***No template type!!!!!!!
> Sort(MyList);
>
>I would think that the function pointer assignment would not compile
>with out using the templae type.
>SORT<int>::FnPtr Sort = &MySortFunc<int>;
>
>But I was able to compile this with out the type on both VC++ and
>Comeau.
>
>So is this code legal, or are the compilers wrong to compile it
>successfully?


It is legal. The compiler instantiates MySortFunc<int> automatically
because it is the only possible candidate. Otherwise, you might have
to disambiguate it. The compiler is very clever in this regard (well,
most modern compilers are, anyway).

--
Bob Hairgrove
(E-Mail Removed)
 
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
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
pointer to SPECIALIZED member function template not legal c++ ? Ingo C++ 2 10-20-2008 04:46 AM
template inherits from template passing its argument is not legal c++ ? nutty C++ 4 06-11-2006 08:52 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Is it legal that a function pointer to "function with default arguments"? Ajax Chelsea C++ 1 12-02-2003 02:01 PM



Advertisments