将bind1st 用于通过引用获取参数的方法

发布于 2024-12-10 22:06:53 字数 1463 浏览 0 评论 0原文

我有一个像这样的结构:

struct A {
    void i(int i) {}
    void s(string const &s) {}
};

现在当我尝试这个时:

bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");

第一行编译正常,但第二行生成错误:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(299): error C2535: 'void std::binder1st<_Fn2>::operator ()(const std::basic_string<_Elem,_Traits,_Ax> &) const' : member function already defined or declared
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>,
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(293) : see declaration of 'std::binder1st<_Fn2>::operator ()'
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]
          c:\work\sources\exception\test\exception\main.cpp(33) : see reference to class template instantiation 'std::binder1st<_Fn2>' being compiled
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]

可能是什么问题?我该如何解决它?

编辑:

似乎任何参考参数都是一个问题。因此,如果我将 i 方法更改为 void i(int &i) {} 我会收到类似的错误。

I have a struct like this:

struct A {
    void i(int i) {}
    void s(string const &s) {}
};

Now when I try this:

bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");

The first line compiles OK, but the second generates an error:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(299): error C2535: 'void std::binder1st<_Fn2>::operator ()(const std::basic_string<_Elem,_Traits,_Ax> &) const' : member function already defined or declared
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>,
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(293) : see declaration of 'std::binder1st<_Fn2>::operator ()'
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]
          c:\work\sources\exception\test\exception\main.cpp(33) : see reference to class template instantiation 'std::binder1st<_Fn2>' being compiled
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]

What could be the problem? How could I fix it?

Edit:

It seems that any reference argument is a problem. So if I change the i method to void i(int &i) {} I get a similar error.

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

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

发布评论

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

评论(4

慢慢从新开始 2024-12-17 22:06:53

std::bind1st 和 std::bind2nd 不接受采用引用参数的函子,因为它们本身形成对这些参数的引用。您可以

  1. 使用指针作为函数输入而不是引用,
  2. 使用 boost::bind
  3. 接受复制字符串的性能成本

std::bind1st and std::bind2nd don't accept functors which take reference arguments, because they themselves form references to these arguments. You can

  1. use pointers for your function inputs instead of references
  2. use boost::bind
  3. accept the performance cost of copying the string
深白境迁sunset 2024-12-17 22:06:53

该问题是库规范中的缺陷。

查看针对 gcc 的错误报告以及由此产生的讨论: Bug 37811 - bind1st 失败带有引用参数的 mem_fun

C++03 缺乏构建完美绑定库的工具。此问题已在 C++11 中通过完美转发和 std::bind 修复。

The issue is a defect in the library specification.

Take a look at this bug report against gcc and the resulting discussion: Bug 37811 - bind1st fails on mem_fun with reference argument

C++03 lacked the facilities to build a perfect bind library. This issue is fixed in C++11 with perfect forwarding and std::bind.

宫墨修音 2024-12-17 22:06:53

看看这篇帖子,其中解释了模板参数的要求。
正如您已经假设对 std::string 的引用是问题所在。它不是有效的模板参数。

Look at this post were the requirements of template parameters are explained.
As you already assumed the reference to the std::string is the problem. It's not a valid template parameter.

美男兮 2024-12-17 22:06:53

通过指针更改引用

#include <functional>
#include <string>

struct A {
    void i(int i) {}
    void s(const std::string *s) {}
};


int main(void)
{
    A a;
    std::bind1st(std::mem_fun(&A::i), &a)(0);
    const std::string s("");
    std::bind1st(std::mem_fun(&A::s), &a)(&s);
}

Change references by pointers

#include <functional>
#include <string>

struct A {
    void i(int i) {}
    void s(const std::string *s) {}
};


int main(void)
{
    A a;
    std::bind1st(std::mem_fun(&A::i), &a)(0);
    const std::string s("");
    std::bind1st(std::mem_fun(&A::s), &a)(&s);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文