boost :: bind не работает с аргументом указателя - PullRequest
3 голосов
/ 02 ноября 2010

У меня есть эта простая программа.Здесь я пытаюсь связать функцию-член с объектом и вызвать позже с аргументами, необходимыми для вызова функции-члена.Когда функция-член взяла указатель на целое число , 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; 
}

Ответы [ 2 ]

2 голосов
/ 02 ноября 2010

Первым аргументом для привязки должен быть указатель на объект, к которому применяется нестатический метод:

cout << bind(&X::f, &x, _1)(&j) << endl;  
1 голос
/ 02 ноября 2010

Я проверил, что код проблемы работает с 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)
}
...