Variables

| Variables |

Values are immutable constants which cannot be modified at all. Attempting to do so will result in a compile time error. When we want the function to modify the parameter, we use a variable instead. For instance, imagine a curryable (lazy) function plus_assign:

`

        plus_assign(x, y) { x += y; }

`

Here, we want the first function argument x to be mutable. Obviously, we cannot write:

`

        plus_assign(1, 2) // error first argument is immutable

`

In C++, we can pass in a reference to a variable as the first argument in our example above. Yet, by default, the Phoenix framework forces arguments passed to curryable functions to be constant immutable values. To achive our intent, we use the variable<T> class. This is similar to the value<T> class above but instead holds a reference to a variable instead. For example:

`

        int i_;
        actor<variable<int> > i = i_;

`

now, we can use our actor<variable<int> > 'i' as argument to the plus_assign lazy function:

`

        plus_assign(i, 2)

`

A shortcut is the var(v) utility function. The expression above is also equivalent to:

`

        plus_assign(var(i_), 2)

`

Lazy variables are actors. As such, variables can be evaluated through the actor's operator(). Such invocation gives the variables's identity. Example:

`

        int i = 3;
        char const* s = "Hello World";
        cout << var(i)() << var(s)();

`

prints out "3 Hello World"

Finally, another free function const(cv) may also be used. const(cv) creates an actor<variable<T const&> > object where the data is referenced using a constant reference. This is similar to value<T> but when the data to be passed as argument to a function is heavy and expensive to copy by value, the const(cv) offers a low overhead alternative.

Copyright © 2001-2002 Joel de Guzman
Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)