Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Is is possible to pass a function name as a parameter to a template using C++/CLI

I want to use a template function to do some validation where the first parameter will be a string or integer, the second and third will always be dates in string format and the third a fuction name. The parameters will be operated on and the result passed into the function supplied as a paramenter.

This validation function will be accessed throughout my system and where it is called from will dictate the function name passed in.

This is the my first opportunity to apply a template in a real world scenario, so very excited by the possibilities presented.

I will gladly accept suggestions with either a C++/CLI or C# orientation and translate any C# myself.
Posted
Updated 27-Jan-11 23:18pm
v4
Comments
Manas Bhardwaj 28-Jan-11 4:19am    
removed c# tag
ARopo 28-Jan-11 5:00am    
Not sure what exactly it is you want to do. do you want to write a template class, a template function or something else? You might be better just trying it and then posting the code if there is a problem with it.

Function names in C/C++ are actually function pointers. And yes, of course, you may pass a function pointer to a template function.
:)
 
Share this answer
 
Comments
ARopo 28-Jan-11 5:23am    
You can pass function pointer to any function template or not. But not as a template parameter(this should be a type could be a type of function pointer) template parameters being the ones in angle brackets <t1, t2=""> as opposed to function parameters in normal brackets (T1 arg1,T2 arg3)
CPallini 28-Jan-11 5:45am    
Of course I intended it as (template) function argument. Anyway, you're wrong, try:

#include <iostream>
#include <string>

using namespace std;

void show(string s)
{
cout << "foo" << endl;
}

typedef void (*PUNARYFUN)(string s);

template <PUNARYFUN p>
void _apply(string s)
{
p(s);
}

int main()
{
_apply<show>(string("foo"));
}
ARopo 28-Jan-11 6:33am    
Oh year, thanks for the code you learn something everyday. +5 for the comment
Ger Hayden 28-Jan-11 7:05am    
Will give this a shot this afternoon
Espen Harlinn 28-Jan-11 13:50pm    
5+ and it gets even better with template metaprogramming/c++ template polymorphism http://en.wikipedia.org/wiki/Template_metaprogramming
OK, forgive the naming convention - I'll update it later, but so far this is what I've got, and I am confident it will give me what I want when used in a template:

.h
delegate bool DateDelegate(MySqlConnection^, String^, String^, String^, int);
    int Check_Date(DateDelegate^ x,MySqlConnection^, String^, String^, String^, int);


then in the .cpp
DateDelegate^ dtDel;
dtDel = gcnew DateDelegate(&CComs_UM::CheckExists_UserMaster);
Check_Date(dtDel,
           m_LocalConnection,
           m_OrigUsername,
           nullptr,
           nullptr,
           0);

:
:
int User::frmUserMaster::Check_Date(DateDelegate^ x,MySqlConnection^ a1, String^ a2, String^ a3, String^ a4, int a5 )
{
    x->Invoke(a1,a2,a3,a4,a5);
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900