什么是C ++代理? [英] What is a C++ delegate?
问题描述
C ++中委托的一般概念是什么?他们是什么,他们是如何使用和他们用于什么?
我想先以黑箱的方式了解他们,但有点
这不是C ++在其纯净或最干净,但我注意到,我工作的代码库有他们在丰富。我希望能够理解他们,所以我可以使用它们,而不必深入到可怕的嵌套模板awfulness。
这两个代码项目文章解释了我的意思,但不是特别简洁:
在C ++中有很多令人难以置信的选择来实现代表。
选项1:函子: p>可以通过实现 operator()创建一个函数对象
struct Functor
{
//正常类/结构成员
int operator()(double d)//任意返回类型和参数列表
{
return(int)d + 1;
}
};
//使用:
Functor f;
int i = f(3.14);
选项二:lambda表达式a href =http://en.wikipedia.org/wiki/C++11>仅限C ++ 11 )
//语法大致为:[capture](parameter list) - >返回类型{block}
//存在一些快捷键
auto func = [](int i) - > double {return 2 * i / 1.15; };
double d = func(1);
选项3: strong>
int f(double d){...}
typedef int(* MyFuncT) );
MyFuncT fp =& f;
int a = fp(3.14);
选项4: (最快解决方案)
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 :: placeholders :: _ 1);
// auto f = std :: bind(...);在C ++中11
选项7 :templates
接受任何与参数列表匹配的内容。
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屋!