C++0x 函数、绑定和成员

发布于 2024-11-10 12:19:30 字数 2181 浏览 3 评论 0原文

我尝试遵循 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)

关于霍华德建议,我尝试过:-) order of args in绑定

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 :-) order of args in bind

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2024-11-17 12:19:30
f = bind(g, &x,_1,_2); // OK

占位符引用返回的绑定对象中的参数位置。不索引 g 的参数。

f = bind(g, &x,_1,_2); // OK

The placeholders refer to the argument positions in the returned bind object. The don't index g's parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文