


Binding Member Variables

#include <boost/phoenix/bind/bind_member_variable.hpp>

Member variables can also be bound much like member functions. Member variables are not functions. Yet, like the ref(x) that acts like a nullary function returning a reference to the data, member variables, when bound, act like a unary function, taking in a pointer or reference to an object as its argument and returning a reference to the bound member variable. For instance, given:

struct xyz
    int v;

xyz::v can be bound as:

bind(&xyz::v, obj) // obj is an xyz object

As noted, just like the bound member function, a bound member variable also expects the first (and only) argument to be a pointer or reference to an object. The object (reference or pointer) can be lazily bound. Examples:

xyz obj;
bind(&xyz::v, arg1)             // arg1.v
bind(&xyz::v, obj)              // obj.v
bind(&xyz::v, arg1)(obj) = 4    // obj.v = 4