繁体   English   中英

当我向作为std :: mem_fun()参数的成员函数添加引用参数时,为什么编译错误?

[英]why did it compile errorly when i added a reference argument to a member function which is the argument to std::mem_fun()?

首先,我有一个片段如下:

struct D
{

  int sum;

  D():sum(0){accum();}

  void incre(int arg){sum+=arg;}

  void accum()
  {
    int arr[]={1,2,3,4,5};

    std::for_each(arr,arr+ sizeof(arr)/sizeof(int),
                  std::bind1st(std::mem_fun(&D::incre),this));

    cout << sum <<endl;
  }
};

int main()
{
  D();
}

据我改变成员函数编译后properly.But incre

void incre(int &  arg){sum+=arg;}

它产生了错误,比如

typename _Operation::result_type std::binder1st<_Operation>::operator()
    (typename _Operation::second_argument_type&) const [with _Operation = 
    std::mem_fun1_t<void, D, int&>]’ cannot be overloaded

你有什么想法发生了什么? 我会感激任何帮助。

发生的事情是bind1st和mem_fun 不能与所有平台上的引用一起使用

你可以将它与boost :: bind一起使用

std::for_each( arr, arr + sizeof(arr)/sizeof(int), 
   boost::bind( &D::incre, this, _1 ) );

并且看起来GNU已经确定以上是一个足够好的解决方法来将错误标记为“无法修复”。

在您的情况下,您可以通过值传入。 您也可以愉快地将指针传递给这些函数。

顺便说一句,你在做什么应该工作。

按值传递可能无法修复它,因为您正在调用非const成员函数。 关于非const成员函数也存在问题。

你的另一种选择当然是使用std :: accumulate而不是std :: for_each,这适用于你正在运行你的集合生成某些东西的特殊情况。 我通常喜欢使用累积的方式是:

Result r;
Result* binaryfunc( Result*, T value ); // returns the same pointer passed in
std::accumulate( coll.begin(), coll.end(), binaryfunc, &r );

这避免了在每次迭代时复制“结果”。 这里不需要使用bind1st或mem_fun,因此如果您通过引用传递值,则不会出现问题。

问题是在内部,mem_fun尝试将其作为参数类型设置为成员函数的参数类型的const引用。 如果你使函数接受引用,那么它会尝试创建对引用的引用,这在C ++中是非法的。 这是库中的已知缺陷,并且正在通过将出现在C ++ 0x中的新绑定函数进行补救。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM