473,804 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer to template function

Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template _function] fptr = fooFunc; // <--

I couldn't find any info here, not even in the forums:
http://www.function-pointer.org/

Thanks,
Suzanne

Jul 22 '05 #1
5 2965
Suzanne Vogel wrote:
Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template _function] fptr = fooFunc; // <--

I couldn't find any info here, not even in the forums:
http://www.function-pointer.org/

Thanks,
Suzanne

What seems to be the problem ?

The example you start with has no dependance on the template.
I created a second example that might be more like what you're after ...
see below.

template<class T> int fooFunc()
{
return 0;
}

class x;

int (* ptr_fooFunc)() = &fooFunc<x>;
template<class T> T * zooFunc()
{
return 0;
}

template<class T>
struct Stuff
{

static T* (* ptr_zooFunc)();

};

template<class T>
T* (* Stuff<T>::ptr_z ooFunc)() = &zooFunc<T>;

int main()
{
ptr_fooFunc();

(*Stuff<int>::p tr_zooFunc)();

}

Jul 22 '05 #2
On Fri, 16 Jan 2004 18:46:37 -0500 in comp.lang.c++, Suzanne Vogel
<su************ *@hotmail.com> was alleged to have written:
Is it possible to store a pointer to a template function?

eg,
template<cla ss T> fooFunc() {...}

[ptr_to_template _function] fptr = fooFunc; // <--


No. A pointer must be a pointer to a specific type. A function
pointer must be a pointer to a specific function signature. Template
instantiations have basically no relationship to each other. Related
types are created by inheritance. What are you trying to accomplish?

Jul 22 '05 #3
Suzanne Vogel wrote:
Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template _function] fptr = fooFunc; // <--
...


Strictly speaking, 'fooFunc' is not a template function. It is a
function template. It is not possible to have a pointer to function
template. However, once the template is specialized it becomes ordinary
function and you can create a pointer to it.

There are different ways to specialize a function template. It can be
done by explicit specification of template arguments. Or it can be done
implicitly by compiler (template argument deduction). In fact, when you
do something like this

SomePtrToFuncti onType fptr = fooFunc;

(where 'fooFunc' is a function template) the compiler will attempt to
perform template argument deduction based on the target type
('SomePtrToFunc tionType') in order to obtain compatible specialization
of 'fooFunc' template.

But again, even if it works, if creates a pointer to a concrete
specialization of the template, not to the function template itself.
There is no such thing in C++ as 'pointer to template'.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4
Thanks to all those who responded...

David Harmon wrote:
On Fri, 16 Jan 2004 18:46:37 -0500 in comp.lang.c++, Suzanne Vogel
<su************ *@hotmail.com> was alleged to have written:
Is it possible to store a pointer to a template function?

eg,
template<clas s T> fooFunc() {...}

[ptr_to_template _function] fptr = fooFunc; // <--

No. A pointer must be a pointer to a specific type. A function
pointer must be a pointer to a specific function signature. Template
instantiations have basically no relationship to each other. Related
types are created by inheritance. What are you trying to accomplish?


Okay, thanks.

Here's what I was trying to accomplish: I wanted to store a hashmap of
pointers to template functions, indexed by classname:

std::hash_map< std::string, [ptr_to_template _func] > mFuncPtrs;

Each of these pointers to a template function came from here:

template<class T>
class Wrapper
{
public:
template<class S>
void f() {.....} //acts on types S and T together
}

To store a pointer to a template function for class Foo, I'd do this:

class Foo {...}

mFuncPtrs["Foo"] = [ptr_to_Wrapper< Foo>::f()];

But, apparently this is impossible.

Suzanne

Jul 22 '05 #5
On Sat, 17 Jan 2004 01:23:37 -0500 in comp.lang.c++, Suzanne Vogel
<su************ *@hotmail.com> was alleged to have written:
Here's what I was trying to accomplish: I wanted to store a hashmap of
pointers to template functions, indexed by classname:

std::hash_ma p< std::string, [ptr_to_template _func] > mFuncPtrs;


OK, I don't know if this idea gets you closer or not:
Create a base class. The function you are going to call is a virtual
function in the base class. The template classes are derived from the
base, and override the virtual function. The map contains (base*)
pointers.

I don't see how you intend to get from the function pointer to a
type-safe invocation of it. If you wish to throw away type safety, then
there are plenty of ways.

Jul 22 '05 #6

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

Similar topics

4
2145
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
4
4263
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are complementary to one another. So I proceeded to decompose the tasks that deal with user options into two functions. Each of the functions do something and towards the end they do supplementary tasks that depend on the server option. The whole things...
15
2237
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the nodes with the following code, but I don't know how to sum them up and return that value to the calling function in main(). I know the algorithm is recursive in nature, but I get gibberish for
11
3439
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
2272
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
35638
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
2458
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
4521
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
3831
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
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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
10326
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...
1
10317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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
9143
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
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.