Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Arguments

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

We use an instance of:

expression::argument<N>::type

to represent the Nth function argument. The argument placeholder acts as an imaginary data-bin where a function argument will be placed.

Predefined Arguments

There are a few predefined instances of expression::argument<N>::type named arg1..argN, and its BLL counterpart _1.._N. (where N is a predefined maximum).

Here are some sample preset definitions of arg1..argN

namespace placeholders
{
    expression::argument<1>::type const arg1 = {};
    expression::argument<2>::type const arg2 = {};
    expression::argument<3>::type const arg3 = {};
}

and its BLL _1.._N style counterparts:

namespace placeholders
{
    expression::argument<1>::type const _1 = {};
    expression::argument<2>::type const _2 = {};
    expression::argument<3>::type const _3 = {};
}
[Note] Note

You can set BOOST_PHOENIX_ARG_LIMIT, the predefined maximum placeholder index. By default, BOOST_PHOENIX_ARG_LIMIT is set to BOOST_PHOENIX_LIMIT (See Actor).

User Defined Arguments

When appropriate, you can define your own argument names. For example:

expression::argument<1>::type x; // note one based index

x may now be used as a parameter to a lazy function:

add(x, 6)

which is equivalent to:

add(arg1, 6)
Evaluating an Argument

An argument, when evaluated, selects the Nth argument from the those passed in by the client.

For example:

char        c = 'A';
int         i = 123;
const char* s = "Hello World";

cout << arg1(c) << endl;        //  Get the 1st argument: c
cout << arg1(i, s) << endl;     //  Get the 1st argument: i
cout << arg2(i, s) << endl;     //  Get the 2nd argument: s

will print out:

A
123
Hello World
Extra Arguments

In C and C++, a function can have extra arguments that are not at all used by the function body itself. These extra arguments are simply ignored.

Phoenix also allows extra arguments to be passed. For example, recall our original add function:

add(arg1, arg2)

We know now that partially applying this function results to a function that expects 2 arguments. However, the library is a bit more lenient and allows the caller to supply more arguments than is actually required. Thus, add actually allows 2 or more arguments. For instance, with:

add(arg1, arg2)(x, y, z)

the third argument z is ignored. Taking this further, in-between arguments are also ignored. Example:

add(arg1, arg5)(a, b, c, d, e)

Here, arguments b, c, and d are ignored. The function add takes in the first argument (arg1) and the fifth argument (arg5).

[Note] Note

There are a few reasons why enforcing strict arity is not desirable. A case in point is the callback function. Typical callback functions provide more information than is actually needed. Lambda functions are often used as callbacks.


PrevUpHomeNext