什么是C ++代理? [英] What is a C++ delegate?
问题描述
我想先以黑盒子的方式先了解一下,但有一点关于这些事情的内容的信息也是很棒的。
这不是最纯洁或最干净的C ++,但我注意到我工作的代码库有它们丰富。我希望能够理解它们,所以我可以使用它们,而不必深入研究可怕的嵌套模板的可怕性。
这两个代码项目文章解释了我的意思,但不是特别简洁:
你有一个令人难以置信的数量的选择来实现C ++中的代表。这是我想到的那些。
选项1:functors: p>
可以通过实现 operator()
struct Functor
{
//正常类/结构成员
int运算符()(double d)//任意返回类型和参数列表
{
return(int)d + 1;
}
};
//使用:
Functor f;
int i = f(3.14);
选项2: a href =http://en.wikipedia.org/wiki/C++11 =noreferrer> C ++ 11 )
//语法大致为:[capture](参数列表) - >返回类型{block}
//存在一些快捷方式
auto func = [](int i) - > double {return 2 * i / 1.15; };
double d = func(1);
选项3:函数指针
int f(double d){...}
typedef int(* MyFuncT)(double d );
MyFuncT fp =& f;
int a = fp(3.14);
选项4:指向成员函数的指针(最快的解决方案)
请参阅 Fast C ++ Delegate (在代码项目)。
struct DelegateList
{
int f1(double d){}
int f2(double d){}
};
typedef int(DelegateList :: * DelegateType)(double d);
DelegateType d =& DelegateList :: f1;
DelegateList list;
int a =(list。* d)(3.14);
选项5: std :: function
(或 boost :: function
如果您的标准库不支持)。它是更慢,但它是最灵活的。
#include< functional>
std :: function< int(double)> f = [可以设置为这个答案中的任何东西]
//通常作为另一个函数的参数更有用
选项6:绑定(使用 std :: bind )
允许提前设置一些参数,方便调用成员函数实例。
struct MyClass
{
int DoStuff(double d); //实际上是一个DoStuff(MyClass * this,double d)
};
std :: function< int(double d)> f = std :: bind(& MyClass :: DoStuff,this,std :: placeholder :: _ 1);
// auto f = std :: bind(...);在C ++ 11
选项7 :template
只要符合参数列表,就可以接受任何内容。
template< class FunctionT>
int DoSomething(FunctionT func)
{
return func(3.14);
}
What is the general idea of a delegate in C++? What are they, how are they used and what are they used for?
I'd like to first learn about them in a 'black box' way, but a bit of information on the guts of these things would be great too.
This is not C++ at its purest or cleanest, but I notice that the codebase where I work has them in abundance. I'm hoping to understand them enough, so I can just use them and not have to delve into the horrible nested template awfulness.
These two The Code Project articles explain what I mean but not particularly succinctly:
You have an incredible number of choices to achieve delegates in C++. Here are the ones that came to my mind.
Option 1 : functors:
A function object may be created by implementing operator()
struct Functor
{
// Normal class/struct members
int operator()(double d) // Arbitrary return types and parameter list
{
return (int) d + 1;
}
};
// Use:
Functor f;
int i = f(3.14);
Option 2: lambda expressions (C++11 only)
// Syntax is roughly: [capture](parameter list) -> return type {block}
// Some shortcuts exist
auto func = [](int i) -> double { return 2*i/1.15; };
double d = func(1);
Option 3: function pointers
int f(double d) { ... }
typedef int (*MyFuncT) (double d);
MyFuncT fp = &f;
int a = fp(3.14);
Option 4: pointer to member functions (fastest solution)
See Fast C++ Delegate (on The Code Project).
struct DelegateList
{
int f1(double d) { }
int f2(double d) { }
};
typedef int (DelegateList::* DelegateType)(double d);
DelegateType d = &DelegateList::f1;
DelegateList list;
int a = (list.*d)(3.14);
Option 5: std::function
(or boost::function
if your standard library doesn't support it). It is slower, but it is the most flexible.
#include <functional>
std::function<int(double)> f = [can be set to about anything in this answer]
// Usually more useful as a parameter to another functions
Option 6: binding (using std::bind)
Allows setting some parameters in advance, convenient to call a member function for instance.
struct MyClass
{
int DoStuff(double d); // actually a DoStuff(MyClass* this, double d)
};
std::function<int(double d)> f = std::bind(&MyClass::DoStuff, this, std::placeholders::_1);
// auto f = std::bind(...); in C++11
Option 7: templates
Accept anything as long as it matches the argument list.
template <class FunctionT>
int DoSomething(FunctionT func)
{
return func(3.14);
}
这篇关于什么是C ++代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!