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

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 7339
"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<class 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*******@mariani.ws> wrote in message news:bu********@dispatch.concentric.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*******@mariani.ws> wrote in message news:bu********@dispatch.concentric.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
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...
4
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...
20
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...
11
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...
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...
2
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...
3
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...
50
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): ...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.