References

| Home | Libraries | People | FAQ | More

#### References

    #include <boost/phoenix/core/reference.hpp>

Values are immutable constants. Attempting to modify a value will result in a compile time error. When we want the function to modify the parameter, we use a reference instead. For instance, imagine a lazy function `add_assign`:

    void add_assign(T& x, T y) { x += y; } // pseudo code

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

    add_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 library forces arguments passed to partially applied functions to be immutable values (see Values). To achieve our intent, we use:

    expression::reference<T>::type

This is similar to `expression::value<T>::type` before but instead holds a reference to a variable.

We normally don't instantiate `expression::reference<T>::type` objects directly. Instead we use:

    ref(v)

For example (where `i` is an `int` variable):

    add_assign(ref(i), 2)

###### Evaluating a Reference

References are actors. Hence, references can be evaluated. Such invocation gives the reference's identity. Example:

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

prints out "3 Hello World"

###### Constant References

Another free function

    cref(cv)

may also be used. `cref(cv)` creates an `expression::reference<T const>::type` object. This is similar to `expression::value<T>::type` but when the data to be passed as argument to a function is heavy and expensive to copy by value, the `cref(cv)` offers a lighter alternative.