473,794 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing pointer to template function as argument to pointer to template function

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 fits in a framework which notifies listeners before
and after the main tasks and so the notification scheme for the
listeners are separate policies as well.

The whole thing is combinatorially complex and hence my decision to
split things up using templates as policies. Here is where I run into
my question: It is exemplified by the following simple code:

#include <iostream>

// main task function
template<class FuncPtr>
void Foo(FuncPtr ptr)
{
ptr();
}

// want to pass an instance of this to Foo
template<class X>
void Bar(X x)
{
x();
}

// want to pass pointer to this to Bar
void G()
{
std::wcout << "From G!" << std::endl;
}

int main()
{
Foo(&Bar(&G)); // doesn't work, coz Bar(&G) is a call to
// function Bar with pointer to G and
// inferred template argument

Foo(&Bar<G>); // don't work becuase the compiler could
// not infer the template argument
// type for overloadl function type from
// overloaded function type

Foo< Bar<G> >( &Bar<G> ); // doesn't work I suppose because
// I should pass the template type as
// pointer to Bar<G> but the compiler
// complains as "invalid template
// argument for FuncPtr; type
// expected"

typedef void (*GPtr) ();
typedef void (*BarGPtr)(GPtr ) ;
Foo< BarGPtr >( &Bar<G> ); // doesn't work; compiler complains
// that param 1 for Foo cannot be
// converted from type void(T1) to
// BarGPtr. I suppose T1 is a temporary
// type?
return 0;
}

Any ideas on this?

thanks,

-vijai.

Nov 8 '05 #1
4 4260
Vijai Kalyan wrote:
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 fits in a framework which notifies listeners before
and after the main tasks and so the notification scheme for the
listeners are separate policies as well.

The whole thing is combinatorially complex and hence my decision to
split things up using templates as policies. Here is where I run into
my question: It is exemplified by the following simple code:

#include <iostream>

// main task function
template<class FuncPtr>
void Foo(FuncPtr ptr)
{
ptr();
}

// want to pass an instance of this to Foo
template<class X>
void Bar(X x)
{
x();
}

// want to pass pointer to this to Bar
void G()
{
std::wcout << "From G!" << std::endl;
}

int main()
{
Foo(&Bar(&G)); // doesn't work, coz Bar(&G) is a call to
// function Bar with pointer to G and
// inferred template argument

Foo(&Bar<G>); // don't work becuase the compiler could
// not infer the template argument
// type for overloadl function type from
// overloaded function type

Foo< Bar<G> >( &Bar<G> ); // doesn't work I suppose because
// I should pass the template type as
// pointer to Bar<G> but the compiler
// complains as "invalid template
// argument for FuncPtr; type
// expected"

typedef void (*GPtr) ();
typedef void (*BarGPtr)(GPtr ) ;
Foo< BarGPtr >( &Bar<G> ); // doesn't work; compiler complains
// that param 1 for Foo cannot be
// converted from type void(T1) to
// BarGPtr. I suppose T1 is a temporary
// type?
return 0;
}

Any ideas on this?

thanks,

-vijai.


How about using a functor instead (only changed code is shown):

template<class X>
struct Bar
{
X x_;
Bar( const X& x ) : x_( x ) {}
void operator()()
{
x_();
}
};

int main()
{
typedef void (*GPtr)();
Foo( Bar<GPtr>(G) );
return 0;
}

Cheers! --M

Nov 8 '05 #2
Yep, I thought about a functor, but I am not sure about using it purely
from an aesthetic perspective. But thanks anyway. If nobody comes up
with something then I will use a functor, but I still hope someone will
know what the syntax is ... I googled it, but came up with pageful of
results about function templates but not of pointers to them. It sure
would be nice to know!

regards,

-vijai.

Nov 8 '05 #3
"Vijai Kalyan" <vi**********@g mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com
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 fits in a framework which notifies listeners before
and after the main tasks and so the notification scheme for the
listeners are separate policies as well.

The whole thing is combinatorially complex and hence my decision to
split things up using templates as policies. Here is where I run into
my question: It is exemplified by the following simple code:

#include <iostream>

// main task function
template<class FuncPtr>
void Foo(FuncPtr ptr)
{
ptr();
}

// want to pass an instance of this to Foo
template<class X>
void Bar(X x)
{
x();
}


In that case, Foo is defined wrongly. If you pass an instance of Bar to Foo,
then ptr will be &Bar<somethi ng> so

ptr();

translates into

Bar<something>( );

which is wrong because Bar<something> has an X parameter. You must call:

ptr(x);

However, your Foo function isn't set up to be able to make sense of this x
argument. Try this

template<class BarFuncPtr, class GFunctPtr>
void Foo(BarFuncPtr bptr, GFunctPtr gptr)
{
bptr(gptr);
}

// want to pass an instance of this to Foo
template<class X>
void Bar(X x)
{
x();
}

// want to pass pointer to this to Bar --- via Foo
void G()
{
std::wcout << "From G!" << std::endl;
}

int main()
{
typedef void (*GPtr) ();
Foo(&Bar<GPtr>, &G);
return 0;
}
--
John Carson

Nov 8 '05 #4
That's interesting. So, this means that Foo has to have knowledge of
the type and structure of it's argument in order to understand that its
second parameter has to be passed as an argument to its first
parameter. But, that makes them strongly coupled right? What if I later
wanted to use Foo with a function that was not a template?

Anyway, this is interesting to know.

Thanks for your help ... I finally went with Functors anyway!

regards,

-vijai.

Nov 8 '05 #5

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

Similar topics

5
2964
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums: http://www.function-pointer.org/
58
10183
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
6
1752
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
17
3605
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
1
3982
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
0
1941
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from. so here is the error I'm getting: TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
2
2379
by: webinfinite | last post by:
This is my first post here, please tell me if I did anything wrong. in the following code snippet: 1. template <class In, class Pred> 2. In find_if(In begin, In end, Pred f) { 3. while (begin != end && !f(*begin)) 4. ++begin; 5. return begin; 6. }
3
2457
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:
18
3274
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
9518
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
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10000
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
9035
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...
0
6777
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
5436
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
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
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
3
2919
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.