473,651 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template Function use as function argument problem,thanks.

#include <iostream>
using namespace std;

typedef bool (*CallableFunct ion)(int argc,char* argv[]);

void DefineMyFunctio n(const char* name,CallableFu nction func){
//here do the define action.
}

template<typena me R,typename A,R (*pFunc)(A)>
bool CallableFunctio nProxy(int argc,char* argv[]){
A a = A(); // this is demo only
pFunc(a);
return true;
}

template<typena me R,typename A>
void Def(const char* name,R (*pFunc)(A)){
DefineMyFunctio n(name,Callable FunctionProxy<R ,A,pFunc>);
}

int alert(int){
cout << "From alert" << endl;
return 0;
}

int main(int argc,char* argv[])
{
CallableFunctio nProxy<int,int, alert>(argc,arg v);
//Def("alert",ale rt); //error here
return 0;
}
this program compile and execute as respected.
but when I uncomment the line marked with "error here" in function
"main" the following compile error occur, could any one known what is
error:

D:\Program\Code Blocks\Projects \firstCpp\main. cpp:29: instantiated from
here
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:18: error: `pFunc' is
not a valid template argument
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:18: error: it must be
the address of a function with external linkage
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:18: error: `pFunc' is
not a valid template argument
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:18: error: it must be
the address of a function with external linkage
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:18: error: `pFunc' is
not a valid template argument
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:18: error: it must be
the address of a function with external linkage
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:18: error: no matches
converting function `CallableFuncti onProxy' to type `bool (*)(int,
char**)'
D:\Program\Code Blocks\Projects \firstCpp\main. cpp:10: error: candidates
are: template<class R, class A, R (*pFunc)(A)bool
CallableFunctio nProxy(int, char**)
:: === Build finished: 8 errors, 1 warnings ===

Jun 13 '07 #1
3 1605
On 13 Jun, 07:23, linarin <lin.jian...@gm ail.comwrote:
template<typena me R,typename A>
void Def(const char* name,R (*pFunc)(A)){
pFunc here is a run-time value, it can not be used
as template parameter.
DefineMyFunctio n(name,Callable FunctionProxy<R ,A,pFunc>);
you could change Def to take pFunc as a template
parameter the same way you use it in
CallableFunctio nProxy

template<typena me R,typename A, R (*pFunc)(A)>
void Def(const char* name){
DefineMyFunctio n(name,Callable FunctionProxy<R ,A,pFunc>);
}

but then you have to call it

Def<int, int, &alert>("alert" );

(I have no idea what you're trying to achieve)
you should check boost::function (or std::tr1::funct ion
if you have tr1)

DS

Jun 13 '07 #2
Oh,thanks.

but how boost.python is implemented. I am trying port the
boost.python to SpiderMonkey.

have you any idea?

Jun 14 '07 #3
On 14 Jun, 01:24, linarin <lin.jian...@gm ail.comwrote:
Oh,thanks.

but how boost.python is implemented. I am trying port the
boost.python to SpiderMonkey.

have you any idea?
no, but you can download boost code and check
for yourself.

DS

Jun 14 '07 #4

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

Similar topics

2
6269
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short, here's the situation (ignore missing namespaces, etc, since I'm not cut-pasting this..) // a.h template <class B>
2
2070
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with something like below: class PMinimum { public: template <typename T> bool operator()(const T & a, const T & b)
14
2892
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); template<typename T>
3
2747
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the default value, which is always_true(). but gcc says: no matching function for call to `x::do_something()' and msvs says: could not deduce template argument for '_Tp'
2
2503
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
3
3933
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. template<class Type> class base { public: base& operator/=(const base&); Type *image;
5
1739
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; // function object
16
3950
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
2
6629
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
8349
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
8275
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
8795
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...
1
8460
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
8576
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
7296
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
5609
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
4143
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...
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.