C++0x 函数、绑定和成员
我尝试遵循 Bjarne Stroustups 的解释function
模板。我特别研究了c函数指针、函子、lambda和成员函数指针的互换性
给定定义:
struct IntDiv { // functor
float operator()(int x, int y) const
{ return ((float)x)/y; }
};
// function pointer
float cfunc(int x, int y) { return (float)x+y; }
struct X { // member function
float mem(int x, int y) const { return ...; }
};
using namespace placeholders; // _1, _2, ...
我想将一切可能的值分配给 function
:
int main() {
// declare function object
function<float (int x, int y)> f;
//== functor ==
f = IntDiv{}; // OK
//== lambda ==
f = [](int x,int y)->float { return ((float)y)/x; }; // OK
//== funcp ==
f = &cfunc; // OK
// derived from bjarnes faq:
function<float(X*,int,int)> g; // extra argument 'this'
g = &X::mem; // set to memer function
X x{}; // member function calls need a 'this'
cout << g(&x, 7,8); // x->mem(7,8), OK.
//== member function ==
f = bind(g, &x,_2,_3); // ERROR
}
最后一行给出了典型的不可读的编译器模板错误。 叹息。
我想将 f
绑定到现有的 x
实例成员函数,以便只留下签名 float(int,int)
。
应该用什么行代替
f = bind(g, &x,_2,_3);
...或者哪里还有错误?
背景:
这里是 Bjarnes 使用 bind
和 的示例>function
带有成员函数:
struct X {
int foo(int);
};
function<int (X*, int)> f;
f = &X::foo; // pointer to member
X x;
int v = f(&x, 5); // call X::foo() for x with 5
function<int (int)> ff = std::bind(f,&x,_1)
我认为 bind
是这样使用的:未分配的位置得到占位符
,其余的填写bind
。 _1
应该得到这个
,然后`?因此最后一行是:
function<int (int)> ff = std::bind(f,&x,_2)
关于霍华德建议,我尝试过:-)
I tried to follow Bjarne Stroustups explanation of the function
template. I specifically played with the interchangability of c-function-pointers, functors, lambdas and member-function-pointers
Given the defintions:
struct IntDiv { // functor
float operator()(int x, int y) const
{ return ((float)x)/y; }
};
// function pointer
float cfunc(int x, int y) { return (float)x+y; }
struct X { // member function
float mem(int x, int y) const { return ...; }
};
using namespace placeholders; // _1, _2, ...
I want to assign to function<float(int,int)>
everything possible:
int main() {
// declare function object
function<float (int x, int y)> f;
//== functor ==
f = IntDiv{}; // OK
//== lambda ==
f = [](int x,int y)->float { return ((float)y)/x; }; // OK
//== funcp ==
f = &cfunc; // OK
// derived from bjarnes faq:
function<float(X*,int,int)> g; // extra argument 'this'
g = &X::mem; // set to memer function
X x{}; // member function calls need a 'this'
cout << g(&x, 7,8); // x->mem(7,8), OK.
//== member function ==
f = bind(g, &x,_2,_3); // ERROR
}
And the last line gives a typical unreadable compiler-template-error. sigh.
I want to bind f
to an existing x
instances member function, so that only the signature float(int,int)
is left.
What should be the line instead of
f = bind(g, &x,_2,_3);
...or where else is the error?
Background:
Here comes Bjarnes example for using bind
and function
with a member function:
struct X {
int foo(int);
};
function<int (X*, int)> f;
f = &X::foo; // pointer to member
X x;
int v = f(&x, 5); // call X::foo() for x with 5
function<int (int)> ff = std::bind(f,&x,_1)
I thought bind
is used this way: The un-assigned places get placeholders
, the rest is filled in the bind
. Should _1
nut get this
, then`? And therefore the last line be:
function<int (int)> ff = std::bind(f,&x,_2)
On Howards suggestion below I tried it :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
占位符引用返回的绑定对象中的参数位置。不索引
g
的参数。The placeholders refer to the argument positions in the returned bind object. The don't index
g
's parameters.