473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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?

Mar 19 '06 #1
1 1789
On 19 Mar 2006 05:00:16 -0800, ci******@lycos.com 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
No**********@Home.com
Mar 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jonathan Turkanis | last post by:
Can the syntax for function types be used within a function type declaration as a synonym for the corresponding pointer-to-function type? For instance, is int(char(long)) a valid type...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
3
by: Ingo | last post by:
Hi, the following code: template< typename objectT , typename worldT > void(objectT::* getInvocationFunctionFor( objectT* v ))(worldT&) { return &(objectT::custom_func/*<worldT>*/);
5
by: Noah Roberts | last post by:
template < typename T1, typename T2, T2 T1::*x > struct test1 { }; struct test2 { int x; typedef test1<test2, int, &test2::xtype;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.