The Boost Parameter Library Reference Documentation

Boost

Authors:David Abrahams, Daniel Wallin
Contact:dave@boost-consulting.com, dalwan01@student.umu.se
organization:Boost Consulting
date:$Date: 2005/07/17 19:53:01 $
copyright:Copyright David Abrahams, Daniel Wallin 2005. Distributed under 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)

Contents


1   Preliminaries

This section covers some basic information you'll need to know in order to understand this reference

1.1   Namespaces

In this document, all unqualified identifiers should be assumed to be defined in namespace boost::parameter unless otherwise specified.

1.2   Exceptions

No operation described in this document throws an exception unless otherwise specified.

1.3   Thread Safety

All components of this library can be used safely from multiple threads without synchronization.1

1.4   Typography

Names written in sans serif type represent concepts.

In code blocks, italic type represents unspecified text that satisfies the requirements given in the detailed description that follows the code block.

In a specification of the tokens generated by a macro, bold type is used to highlight the position of the expanded macro argument in the result.

The special character β represents the value of BOOST_PARAMETER_MAX_ARITY.


2   Terminology

keyword
The name of a function parameter.
keyword tag type
A type used to uniquely identify a function parameter. Typically its name will be the same as that of the parameter.
positional argument
An argument passed with no explicit keyword. Its parameter is determined in the usual C++ way: by position with respect to a parameter list.
tag type
Shorthand for “keyword tag type.”
keyword object
An instance of keyword <T> for some tag type T.
tagged reference

An object whose type is associated with a keyword tag type (the object's keyword), and that holds a reference (to the object's value).

As a shorthand, a “tagged reference to x” means a tagged reference whose value is x.

tagged default
A tagged reference whose value represents the value of a default argument.
tagged lazy default
A tagged reference whose value, when invoked with no arguments, computes a default argument value.
intended argument type
The intended argument type of a single-element ArgumentPack is the type of its element's value. The intended argument type of any other type X is X itself.

Note

In this reference, we will use concept names (and other names) to describe both types and objects, depending on context. So for example, “an ArgumentPack” can refer to a type that models ArgumentPack or an object of such a type.


3   Concepts

This section describes the generic type concepts used by the Parameter library.

3.1   ArgumentPack

An ArgumentPack is a collection of tagged references to the actual arguments passed to a function.

Requirements

In the table below,

Any exceptions are thrown from the invocation of w's value will be propagated to the caller.

ArgumentPack requirements
Expression Type Requirements Semantics/Notes
x[u] binding<A,K>::type x contains an element b whose keyword is K Returns b's value (by reference).
x[u] binding<A,L,D>::type none If x contains an element b whose keyword is the same as u's, returns b's value (by reference). Otherwise, returns u's value.
x[w] lazy_binding<A,M,E>::type none If x contains an element b whose keyword is the same as w's, returns b's value (by reference). Otherwise, invokes w's value and returns the result.
x, z Model of ArgumentPack none Returns an ArgumentPack containing all the elements of both x and z.

3.2   ParameterSpec

A ParameterSpec describes the type requirements for arguments corresponding to a given keyword and indicates whether the argument is optional or required. The table below details the allowed forms and describes their condition for satisfaction by an actual argument type. In each row,

ParameterSpec allowed forms and conditions of satisfaction
Type A required Condition A must satisfy
keyword<K> no n/a
optional<K,F> no mpl::apply<F,A>::type::value is true.
required<K,F> yes mpl::apply<F,A>::type::value is true.

The information in a ParameterSpec is used to limit the arguments that will be matched by forwarding functions.


4   Class Templates

4.1   keyword

The type of every keyword object is a specialization of keyword.

Defined in:boost/parameter/keyword.hpp
template <class Tag>
struct keyword
{
    template <class T> ArgumentPack operator=(T& value) const;
    template <class T> ArgumentPack operator=(T const& value) const;

    template <class T> tagged default operator|(T& x) const;
    template <class T> tagged default operator|(T const& x) const;

    template <class F> tagged lazy default operator||(F const&) const;

    static keyword<Tag>& get();
};
operator=
template <class T> ArgumentPack operator=(T& value) const;
template <class T> ArgumentPack operator=(T const& value) const;
Requires:nothing
Returns:an ArgumentPack containing a single tagged reference to value with keyword Tag
operator|
template <class T> tagged default operator|(T& x) const;
template <class T> tagged default operator|(T const& x) const;
Returns:a tagged default with value x and keyword Tag.
operator||
template <class F> tagged lazy default operator||(F const& g) const;
Requires:g() is valid, with type boost::result_of<F()>::type.2
Returns:a tagged lazy default with value g and keyword Tag.
get
static keyword<Tag>& get();
Returns:a “singleton instance”: the same object will be returned on each invocation of get().
Thread Safety:get() can be called from multiple threads simultaneously.

4.2   parameters

Provides an interface for assembling the actual arguments to a forwarding function into an ArgumentPack, in which any positional arguments will be tagged according to the corresponding template argument to parameters.

Defined in:boost/parameter/parameters.hpp
template <class P0 = unspecified, class P1 = unspecified, …class Pβ = unspecified>
struct parameters
{
    template <class A0, class A1 = unspecified, …class Aβ = unspecified>
    struct match
    {
        typedef … type;
    };

    template <class A0>
    ArgumentPack operator()(A0 const& a0) const;

    template <class A0, class A1>
    ArgumentPack operator()(A0 const& a0, A1 const& a1) const; 
   .
   .
   .
 
    template <class A0, class A1, …class Aβ>
    ArgumentPack operator()(A0 const& a0, A1 const& a1, …Aβ const& aβ) const;
};
Requires:P0, P1, … Pβ are models of ParameterSpec.

Note

In this section, Ri and Ki are defined as follows, for any argument type Ai:


if Ai is a result type of keyword<T>::operator=
then
Ki is T
else
Ki is Pi's keyword tag type.
match

A Metafunction used to remove a forwarding function from overload resolution.

Returns:if P0, P1, …Pβ are satisfied (see below), then parameters<P0,P1,…Pβ>. Otherwise, match<A0,A1,…Aβ>::type is not defined.

P0, P1, …Pβ are satisfied if, for every j in 0…β, either:

  • Pj is the unspecified default
  • or, Pj is a specialization of keyword,
  • or, Pj is optional <X,F> and either
    • X is not Ki for any i,
    • or X is some Ki and mpl::apply<F,Ri>::type::value is true
  • or, Pj is required <X,F>, and
    • X is some Ki, and
    • mpl::apply<F,Ri>::type::value is true
operator()
template <class A0> ArgumentPack operator()(A0 const& a0) const; 
  .
  .
  .
 
template <class A0, …class Aβ> ArgumentPack operator()(A0 const& a0, …Aβ const& aβ) const;
Returns:

An ArgumentPack containing, for each ai,

4.3   optional, required

These templates describe the requirements on a function parameter.

Defined in:boost/parameter/parameters.hpp
Specializations model:
 ParameterSpec
template <class Tag, class Predicate = unspecified>
struct optional;

template <class Tag, class Predicate = unspecified>
struct required;

The default value of Predicate is an unspecified Metafunction that returns mpl::true_ for any argument.


5   Metafunctions

A Metafunction is conceptually a function that operates on, and returns, C++ types.

5.1   binding

Returns the result type of indexing an argument pack with a keyword tag type or with a tagged default.

Defined n:boost/parameter/binding.hpp
template <class A, class K, class D = void>
struct binding
{
    typedef … type;
};
Requires:A is a model of ArgumentPack.
Returns:the reference type of the tagged reference in A having keyword tag type K, if any. If no such tagged reference exists, returns D.

5.2   lazy_binding

Returns the result type of indexing an argument pack with a tagged lazy default.

Defined in:boost/parameter/binding.hpp
template <class A, class K, class F>
struct lazy_binding
{
    typedef … type;
};
Requires:A is a model of ArgumentPack.
Returns:the reference type of the tagged reference in A having keyword tag type K, if any. If no such tagged reference exists, returns boost::result_of<F()>::type.2

6   Code Generation Macros

Macros in this section can be used to ease the writing of code using the Parameter libray by eliminating repetitive boilerplate.

6.1   BOOST_PARAMETER_FUN(r,n,l,h,p)

Generates a sequence of forwarding function templates named n, with arities ranging from l to h , returning r, and using p to control overload resolution and assign tags to positional arguments.

Defined in:boost/parameter/macros.hpp
Requires:l and h are nonnegative integer tokens such that l < h
Generates
template <class A1, class A2, …class A##l>
r name(
    A1 const& a1, A2 const& a2, …Al const& xl
  , typename p::match<A1,A2,…Al>::type p = p())
{
   return name_with_named_params(p(x1,x2,…xl));
}

template <class A1, class A2, …class Al, class A##BOOST_PP_INC(l)>
r name(
    A1 const& a1, A2 const& a2, …Al const& xl
  , A##BOOST_PP_INC(l) const& x##BOOST_PP_INC(l)
  , typename p::match<A1,A2,…Al,A##BOOST_PP_INC(l)>::type p = p())
{
   return name_with_named_params(p(x1,x2,…xl,x##BOOST_PP_INC(l)));
} 
  .
  .
  .
 
template <class A1, class A2, …class Ah>
r name(
    A1 const& a1, A2 const& a2, …Ah const& xh
  , typename p::match<A1,A2,…Ah>::type p = p())
{
   return name_with_named_params(p(a1,a2,…ah));
}

6.2   BOOST_PARAMETER_KEYWORD(n,k)

Generates the declaration of a keyword tag type named k in namespace n, and a corresponding keyword object definition in the enclosing namespace.

Defined in:boost/parameter/keyword.hpp
Generates
namespace n { struct k; }
namespace { 
  boost::parameter::keyword<tag-namespace::k>& k
  = boost::parameter::keyword<tag-namespace::k>::get();
}

6.3   BOOST_PARAMETER_MATCH(p,a,x)

Generates a defaulted parameter declaration for a forwarding function.

Defined in:boost/parameter/match.hpp
Requires:

a is a Boost.Preprocessor sequence of the form

(A0)(A1)…(An)
Generates
typename p::match<A0,A1…,An>::type x = p()

7   Configuration Macros

7.1   BOOST_PARAMETER_MAX_ARITY

Determines the maximum number of arguments supported by the library. Will only be #defined by the library if it is not already #defined.

Defined in:boost/parameter/config.hpp
Default Value:5

8   Tutorial

Follow this link to the Boost.Parameter tutorial documentation.


[1]References to tag objects may be initialized multiple times. This scenario can only occur in the presence of threading. Because the C++ standard doesn't consider threading, it doesn't explicitly allow or forbid multiple initialization of references. That said, it's hard to imagine an implementation where it could make a difference.
[2](1, 2) Where BOOST_NO_RESULT_OF is #defined, boost::result_of<F()>::type is replaced by F::result_type.