473,670 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

store pointer to template function?

Is it possible to store a pointer to a template function whose template
type is not known until runtime?

eg,
Can I store a pointer to this?

template<class T> f() {...}

I know how to store pointers to ordinary functions, but not template
functions.

Thanks,
Suzanne

Jul 22 '05 #1
5 7351
"Suzanne Vogel" <su************ *@hotmail.com> wrote in message
news:40******** @news.unc.edu.. .
Is it possible to store a pointer to a template function whose template type is not known until runtime?

eg,
Can I store a pointer to this?

template<class T> f() {...}
Unfortunately, no. You can store pointers to specializations of
template functions, but not the actual template. If you give an
example of your intended use, perhaps we can suggest another way to
accomplish the same thing.

I know how to store pointers to ordinary functions, but not template
functions.


They're stored just like other function pointer. You have to be
careful about getting the address of the correct overload.

Jonathan

Jul 22 '05 #2

"Suzanne Vogel" <su************ *@hotmail.com> wrote in message news:40******** @news.unc.edu.. .
Is it possible to store a pointer to a template function whose template
type is not known until runtime?

eg,
Can I store a pointer to this?

template<class T> f() {...}

No. Templates are instantiated at build time.
You can only get pointers to specific specializations .

Jul 22 '05 #3
Ron Natalie wrote:
"Suzanne Vogel" <su************ *@hotmail.com> wrote in message news:40******** @news.unc.edu.. .
Is it possible to store a pointer to a template function whose template
type is not known until runtime?

eg,
Can I store a pointer to this?

template<clas s T> f() {...}


No. Templates are instantiated at build time.
You can only get pointers to specific specializations .


Except in this case, the function signature is independant of the
template argument. (and the syntax is incorrect too - but that's a
different issue)

This works:
(assuming the template function returns an int)

int (* ptr_fooFunc)() = &f<x>;
Jul 22 '05 #4

"Gianni Mariani" <gi*******@mari ani.ws> wrote in message news:bu******** @dispatch.conce ntric.net...

Except in this case, the function signature is independant of the
template argument. (and the syntax is incorrect too - but that's a
different issue)


It's still the case. There's no requirement that the function signature reflect the template
args. (Except for bugs in certain compilers like VC++ 6). It just means you have to
provide the explicit template argument list.
This works:
(assuming the template function returns an int)

int (* ptr_fooFunc)() = &f<x>;

Doesn't solve the problem the original poster asked for. They wanted to bind the function
by pointer later to a specific specialization. This binds it to the f<x> specialization at initialization
time.

Jul 22 '05 #5
Ron Natalie wrote:
"Gianni Mariani" <gi*******@mari ani.ws> wrote in message news:bu******** @dispatch.conce ntric.net...
This works:
(assuming the template function returns an int)

int (* ptr_fooFunc)() = &f<x>;


Doesn't solve the problem the original poster asked for. They wanted to bind the function
by pointer later to a specific specialization. This binds it to the f<x> specialization at initialization
time.


Correct: doesn't solve the problem. Thanks, Ron. And thanks Gianni, for
the suggestion.

Suzanne

Jul 22 '05 #6

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

Similar topics

14
3523
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers to class Conception instead? How can I do that? And how should I write to declare an iterator to this vector?
4
2135
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived classes
20
5096
by: Manuel | last post by:
Hi. Before all, please excuse me for bad english and for very newbie questions. I hope to don't boring you. I'm trying to write a very simple GUI using openGL. So I'm writing some different widgets classes, like buttons, images, slider, etc... Each class has a draw() method.
11
3425
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
5
2265
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 to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
2
35565
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
3
2446
by: .rhavin grobert | last post by:
guess you have the following: _________________________________________________ template <class T> class CQVector { public: // find an element, returns index or -1 if none is found int find(int id) const; private:
50
4474
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
7
3808
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 event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7421
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6216
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5686
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.