绑定到成员变量

发布于 2024-08-21 11:29:23 字数 731 浏览 1 评论 0 原文

以下来自Boost Bind的示例 对我不起作用:

#include <boost/bind.hpp>

struct A
{
    int data;
};

int main()
{
    A a;
    boost::bind(&A::data, _1)(a) = 1;
}

错误:分配只读位置 'boost::bind [with A1 = boost::arg<1>, M = int, T = A](&A:: data, (::_1, boost::arg<1>())).boost::_bi::bind_t::operator() [其中 A1 = A, R = const int&,F = boost::_mfi::dm,L = boost::_bi::list1; >](((A&)(& a)))'

我做错了什么吗?编译器是g++ 4.4.0

The following example from boost bind does not work for me:

#include <boost/bind.hpp>

struct A
{
    int data;
};

int main()
{
    A a;
    boost::bind(&A::data, _1)(a) = 1;
}

error: assignment of read-only location 'boost::bind [with A1 = boost::arg<1>, M = int, T = A](&A::data, (<unnamed>::_1, boost::arg<1>())).boost::_bi::bind_t<R, F, L>::operator() [with A1 = A, R = const int&, F = boost::_mfi::dm<int, A>, L = boost::_bi::list1<boost::arg<1> >](((A&)(& a)))'

Am I doing anything wrong? The compiler is g++ 4.4.0

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

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

发布评论

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

评论(3

2024-08-28 11:29:23

该绑定表达式的结果类型是 int (或者更确切地说 const int&)。我认为你可以覆盖返回类型 :

boost::bind<int&>(&A::data, _1)(a) = 1;

The result type of that bind expression is int (or rather const int&). I think you can override the return type:

boost::bind<int&>(&A::data, _1)(a) = 1;
贪恋 2024-08-28 11:29:23

UncleBens 的解决方案很好,但我想我应该补充一点,如果您使用 Boost.Lambda,问题就会消失:

#include <boost/lambda/bind.hpp>

struct A {
    int data;
};

int main() {

    namespace bll = boost::lambda;

    A a;
    bll::bind(&A::data, bll::_1)(a) = 1;
}

如果您使用 boost::mem_fn,问题就会消失:

#include <boost/mem_fn.hpp>

struct A {
    int data;
};

int main() {

    boost::mem_fn(&A::data)(a) = 1;
}

UncleBens' solution is fine but I thought I'd add that if you use Boost.Lambda the problem disappears:

#include <boost/lambda/bind.hpp>

struct A {
    int data;
};

int main() {

    namespace bll = boost::lambda;

    A a;
    bll::bind(&A::data, bll::_1)(a) = 1;
}

And so it does if you use boost::mem_fn:

#include <boost/mem_fn.hpp>

struct A {
    int data;
};

int main() {

    boost::mem_fn(&A::data)(a) = 1;
}
娇俏 2024-08-28 11:29:23

我不确定你想做什么,但是 Boost.Bind 真的会重载赋值运算符吗?如果您想使用返回的函数对象将值 1 分配给 a.data 我认为您需要执行类似的操作(另请注意“a”需要通过引用绑定):

#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <cassert>

void foo()
{
    A a;

    boost::bind(&A::data, _1)(boost::ref(a), 1);

    assert(a.data == 1);
}

如果您需要使用赋值我认为使用 Boost.Lambda 或 Boost.Phoenix 会是更好的选择。

I'm not sure what you want to do, but does Boost.Bind really overload the assignment operator? If you'd like to assign the value 1 to a.data using the returned function object I think you need to do something like this (also note that "a" needs to be bound by reference):

#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <cassert>

void foo()
{
    A a;

    boost::bind(&A::data, _1)(boost::ref(a), 1);

    assert(a.data == 1);
}

If you need to use the assignment operator I think that using Boost.Lambda or Boost.Phoenix would be a better choice.

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