473,387 Members | 1,342 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,387 software developers and data experts.

Function pointers & template functions

My question relates to storing the addresses of functions generated by
templates in pointers to functions.

<CODE>

template<class T>
void Global() {}

namespace ANamespace
{
template<class T>
void InANamespace() {}
}

int main()
{
void (*fncPtr)() = Global<int>; //Compiles on both Comeau & MSVC++.NET

fncPtr = ANamespace::InANamespace<int>; //Compiles on Comeau but not
MSVC++.NET

return 0;
}

</CODE>

Using MSVC++.NET line 1 compiles but line 2 does not.

Using the Comeau Online compiler @ http://www.comeaucomputing.com/tryitout/
both lines compile.

I am assuming that Comeau is correct, but you know what they say about making
assumptions.

Can anyone comment on the correct syntax and which compiler is correct?

Thanks.

Jul 22 '05 #1
4 1284

"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m14.aol.com...
<CODE>

template<class T>
void Global() {}

namespace ANamespace
{
template<class T>
void InANamespace() {}
}

int main()
{
void (*fncPtr)() = Global<int>; //Compiles on both Comeau & MSVC++.NET
fncPtr = ANamespace::InANamespace<int>; //Compiles on Comeau but not MSVC++.NET

return 0;
}

</CODE>

Using MSVC++.NET line 1 compiles but line 2 does not.


Compiles fine for me. Perhaps you are using version 7.0?

Jonathan
Jul 22 '05 #2
>Compiles fine for me. Perhaps you are using version 7.0?

7.0.9466 dated 2002.
Jul 22 '05 #3

"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m14.aol.com...
Compiles fine for me. Perhaps you are using version 7.0?


7.0.9466 dated 2002.


7.1 is when it became a first-class compiler.

Jonathan
Jul 22 '05 #4
da*********@aol.com (DaKoadMunky) wrote in message news:<20***************************@mb-m14.aol.com>...
Using MSVC++.NET line 1 compiles but line 2 does not.
...
Can anyone comment on the correct syntax and which compiler is correct?


I've just tried your code using MSVC 7.1 (compiler version 13.10.3077)
and everything is working fine.

harald
Jul 22 '05 #5

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

Similar topics

1
by: Phil | last post by:
Ok, I have a template function for any pointer to type T: template <typename T> void func(T* p) { DoSomethingGeneric(p); } Can I specialize this template for pointers to functions (or...
4
by: Brian | last post by:
Hi all, I am implementing an object that is currently using function pointers for callbacks to let me do something on assignment: template <class T> class MyClass{ private: typedef T& (*...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
2
by: Amadeus W. M. | last post by:
I have a bunch of templated functions: template <class Type_t> double f2(Type_t x) { return 2*x; } template <class Type_t> double f3(Type_t x) { return 3*x; }
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.