boost::bind 不适用于指针参数

发布于 2024-09-30 05:32:33 字数 2581 浏览 2 评论 0原文

我有这个简单的程序。在这里,我尝试将成员函数与对象绑定,并稍后使用成员函数调用所需的参数进行调用。 当成员函数采用指向整数的指针时,gcc 无法编译。使用整数参数可以编译程序。这是 boost::bind 的错误还是我遗漏了什么?

// Doesn't work with pointer
#include <iostream>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
    float& f(int *aa) {
      cout << *aa << endl;
      return f_;
    }
};

int main() {
    X x;

    x.f_=200.1;
    int j = 10;

    cout << bind(&X::f, ref(x), _1)(&j) << endl;    
}

ERROR:
member.cpp: In function ‘int main()’:
member.cpp:22: error: no match for call to ‘(boost::_bi::bind_t<float&,  boost::_mfi::mf1<float&, X, int*>, boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> > >) (int*)’
/usr/include/boost/bind/bind_template.hpp:17: note: candidates are: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() [with R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:23: note:                 typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() const [with R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:29: note:                 typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = int*, R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:35: note:                 typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) const [with A1 = int*, R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]

// Works fine
#include <iostream>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
    float& f(int aa) {
      cout << aa << endl;
      return f_;
    }
};

int main() {
    X x;

    x.f_=200.1;
    int j = 10;

    cout << bind(&X::f, ref(x), _1)(j) << endl; 
}

I have this simple program. Here I try to bind member function with object and call later on with arguments required in member function call.
When the member function taken a pointer to integer, gcc fails to compile. With integer parameter the program gets compiled. Is this a bug with boost::bind or am I missing something?

// Doesn't work with pointer
#include <iostream>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
    float& f(int *aa) {
      cout << *aa << endl;
      return f_;
    }
};

int main() {
    X x;

    x.f_=200.1;
    int j = 10;

    cout << bind(&X::f, ref(x), _1)(&j) << endl;    
}

ERROR:
member.cpp: In function ‘int main()’:
member.cpp:22: error: no match for call to ‘(boost::_bi::bind_t<float&,  boost::_mfi::mf1<float&, X, int*>, boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> > >) (int*)’
/usr/include/boost/bind/bind_template.hpp:17: note: candidates are: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() [with R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:23: note:                 typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() const [with R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:29: note:                 typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = int*, R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:35: note:                 typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) const [with A1 = int*, R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]

// Works fine
#include <iostream>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
    float& f(int aa) {
      cout << aa << endl;
      return f_;
    }
};

int main() {
    X x;

    x.f_=200.1;
    int j = 10;

    cout << bind(&X::f, ref(x), _1)(j) << endl; 
}

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

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

发布评论

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

评论(2

辞别 2024-10-07 05:32:33

bind 的第一个参数应该是指向要应用非静态方法的对象的指针:

cout << bind(&X::f, &x, _1)(&j) << endl;  

The first argument to bind should be the pointer to the object to apply the non-static method to:

cout << bind(&X::f, &x, _1)(&j) << endl;  
思念绕指尖 2024-10-07 05:32:33

我已经测试过问题代码适用于 boost 1.40 和 gcc 4.4.3。我做了进一步的更改来测试更多场景,它们都有效。下面的代码工作完美。

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
  float& f(shared_ptr<int> aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XX
{
  float f_;
  float& f(int* aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XXX
{
  float f_;
  float& f(int* aa, int a) {
    cout << *aa << " " << a << endl;
    return f_;
  }
};


int main() {
  X x;
  XX xx;
  XXX xxx;
  xx.f_ = 2000.11;
  xxx.f_ = 20.11;

  shared_ptr<X> p(new X);

  x.f_=200.1;
  p->f_=20000.11;
  int i = 5;
  int j = 10;

  shared_ptr<int> sh(new int(100));
  x.f(sh);
  cout << bind(&X::f, ref(x), _1)(sh) << endl;      // x.f(i)
  cout << bind(&X::f, &x, _1)(sh) << endl;          //(&x)->f(i)
  cout << bind(&X::f, x, _1)(sh) << endl;           // (internal copy of x).f(i)
  cout << bind(&X::f, p, _1)(sh) << endl;           // (internal copy of p)->f(i)

  cout << bind(&XX::f, ref(xx), _1)(&j) << endl;        // x.f(i)
  cout << bind<float&>(mem_fn(&XXX::f), ref(xxx), _1, _2)(&j, i) << endl;       // x.f(i)
}

I have tested that the problem code works with boost 1.40 and gcc 4.4.3. I made further changes to test some more scenarios and they all work. The following piece of code works perfectly.

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
  float& f(shared_ptr<int> aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XX
{
  float f_;
  float& f(int* aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XXX
{
  float f_;
  float& f(int* aa, int a) {
    cout << *aa << " " << a << endl;
    return f_;
  }
};


int main() {
  X x;
  XX xx;
  XXX xxx;
  xx.f_ = 2000.11;
  xxx.f_ = 20.11;

  shared_ptr<X> p(new X);

  x.f_=200.1;
  p->f_=20000.11;
  int i = 5;
  int j = 10;

  shared_ptr<int> sh(new int(100));
  x.f(sh);
  cout << bind(&X::f, ref(x), _1)(sh) << endl;      // x.f(i)
  cout << bind(&X::f, &x, _1)(sh) << endl;          //(&x)->f(i)
  cout << bind(&X::f, x, _1)(sh) << endl;           // (internal copy of x).f(i)
  cout << bind(&X::f, p, _1)(sh) << endl;           // (internal copy of p)->f(i)

  cout << bind(&XX::f, ref(xx), _1)(&j) << endl;        // x.f(i)
  cout << bind<float&>(mem_fn(&XXX::f), ref(xxx), _1, _2)(&j, i) << endl;       // x.f(i)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文