将函数名称作为参数传递给模板函数 [英] Passing a function name as a paramter to a template function

查看:134
本文介绍了将函数名称作为参数传递给模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用C ++/CLI将函数名称作为参数传递给模板

我想使用模板函数进行一些验证,其中第一个参数将是字符串或整数,第二个和第三个参数始终是字符串格式的日期,第三个参数是功能名称.参数将被操作并将结果作为参数提供给函数.

该验证函数将在我的整个系统中访问,从哪里调用它将确定传入的函数名称.

这是我在现实世界中应用模板的第一个机会,这让我为所提供的可能性感到非常兴奋.

我将很乐意接受有关C ++/CLI或C#方向的建议并亲自翻译任何C#.实际上是函数指针.是的,当然,您可以将函数指针传递给模板函数.
:)


好,请原谅命名约定-稍后我将对其进行更新,但是到目前为止,这是我所拥有的,并且我相信它将为我提供我所拥有的在模板中使用时需要:

.h

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



然后在.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;
}


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.

解决方案

Function names in C/C++ are actually function pointers. And yes, of course, you may pass a function pointer to a template function.
:)


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;
}


这篇关于将函数名称作为参数传递给模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆