There appear to be few
practical uses of operator,(). Bjarne
Stroustrup, The Design and Evolution of C++

The purpose of this
library is to make it easy to fill containers with data by overloading
operator,() and operator()(). These two operators
make it possible to construct lists of values that are then copied into a
container:

A comma-separated list:

vector<int> v; v += 1,2,3,4,5,6,7,8,9;

A parenthesis-separated list:

map<string,int> m; insert( m )( "Bar", 1 )( "Foo", 2 );

These lists are particularly useful in
learning, testing, and prototyping situations, but can also be handy otherwise.
The library comes with predefined operators for the containers of the
standard library, but most functionality will work with any standard
compliant container. The library also makes it possible to extend user
defined types so for example a member function can be called for a list of
values instead of its normal arguments.

Tutorial

Within two minutes you should be able to use this library. The main
components are explained in these sections:

Here we only stuffed constants into the container, but the list can
consists of arbitrary expressions as long as the result of each
expression is convertible to the value_type of the container.

Function operator()()

We do not call operator()() directly, but instead we call a
function that returns a proxy-object that defines operator()().
The function that returns the proxy object is always named after the member
function that is used to copy the values in the list into the container.
So to fill a map with pairs of values you write

Note that operator()() is much more handy when
we need to construct objects using several arguments
(up to five arguments are supported by default, but the limit can be customized).
This is also true for sequences:

Besides push_front() we could also have used
push_back() if the container has a corresponding member
function. Empty parentheses can be used to insert default-constructed
objects, for example,
push_front( deq )()() will insert two default-constructed
str_pair objects.

If operator()() is too cumbersome to use with eg.
push_front()we can also say

Just to make it perfectly clear, the code above is not restricted to the
standard containers, but will work with all standard compliant containers
with the right member function. It is only operator+=() that has been restricted to
the standard containers.

Function list_of()

But what if we need to initialize a container? This is where
list_of() comes into play. With list_of()
we can create anonymous lists that automatically converts to
any container:

If we need to initialize a container adapter, we need to help the compiler a
little by calling to_adapter(). As the second example also shows,
we can use a comma-separated
list with list_of() if we add parenthesis around the
entire right hand side. It is worth noticing that the first argument
of list_of() determines the type of the anonymous list.
In case of the stack, the anonymous list consists of
const char* objects which are then converted to
a stack of string objects. The conversion is always
possible as long as the conversion between the stored types is possible.

You can only use lvalues with ref_list_of() while
cref_list_of() accepts rvalues too. Do not worry about not
specifying exactly the right size; the extra space used is minimal and there
is no runtime overhead associated with it.
You may also use these functions instead of list_of() if speed is
essential.

A "complicated" example

As a last example, let us assume that we need to keep track of the
result of soccer matches. A team will get one point if it wins
and zero otherwise. If there has been played three games in each group, the code might look
like this:

In the first example, notice how the result of list_of()
can be converted automatically to a vector<int> because
insert() knows it expects a vector<int>.
In the second example we can see that list_of() is somewhat
less intelligent since here it needs to be told explicitly what arguments to
expect. (In the future it might be possible to introduce a more intelligent
conversion layer in list_of().)

Functions ptr_push_back(),
ptr_push_front() and ptr_insert()

For use with Boost.Pointer Container
a few special exception-safe functions are provided. Using these function you
do not need to call new manually:

It is worth noticing the way the library is implemented.
A free-standing function (eg. push_back()
or operator+=()) returns a proxy
object which is responsible for the insertion or the assignment. The proxy
object does the insertion or assignment by overloading operator,()
and operator()() and by calling the "insert" function from within
those operators. The "insert" function is typically stored in the proxy object
by using boost::function.

Often overloading
of operator,() is discouraged because it can lead to surprising
results, but the approach taken in this library is safe since the
user never deals with objects that have overloaded operator,()
directly. However, you should be aware of this:

The
expressions in a comma-separated list no longer follow the rules of the
built-in comma-operator. This means that the order of evaluation of
expressions in a comma-separated list is undefined like when one specifies
a list of function arguments.

Most of the code in this document use int in the examples,
but of course it works for arbitrary types as long as they are
Copy Constructible. The inserted data need not be constant data,
but can be variables or data returned from functions; the only requirement
is that the type of the data is convertible to the type stored in the
container.

All forwarding is done by passing objects by const reference.
Originally arguments were passed by value (and still is in
tuple_list_of()). One thing to remember is that references
can be passed by using boost::ref.

Standard containers

In the following three dots (...) will mean
implementation defined.
operator+=() returns a proxy that forwards calls to either
push_back(),insert(), or push()
depending on which operation the container supports.

Note that the extra template argument V2 etc. is
necessary to allow for types convertible to V.

Functions list_of() and
map_list_of()

These two functions are used to construct anonymous
list which can be converted to any standard container
and boost::array<T,sz>.
The object returned by the two
functions is guaranteed to have the interface described below.

Functions repeat() and
repeat_fun()

These two function exist both as free-standing functions and as member functions of the object returned by
list_of() and of list_inserter. The free-standing versions are used to create a
hook for operator,() so we can call the functions in the middle of a comma-list. The member functions
are used when we need to call the functions in the middle of a parenthesis-list. In both cases we have that

Notice how the arguments to operator,() and
operator()() are passed differently to
fun depending of the type of Argument.
So if we only pass one template argument to list_inserter,
we can forward "arbitrary" argument lists of functions. If we pass
two template arguments to list_inserter we can
construct types with "arbitrary" constructors.

And because
a reference to list_inserter is returned, we can
chain argument list together in a very space-efficient manner.

Customizing argument list sizes

This library uses the boost Preprocessor Library to implement overloaded
versions of operator()() and list_of(). By default you
can call these functions with five arguments, but you can also customize this
number by defining a macros before including a header from this library:

The exception guarantees by the library is the same as guarantee as the
guarantee of the function that is forwarded to. For standard
containers this means that the
strong guarantee is given for a single insertions and that the basic guarantee
is given for many insertions (provided that the object being copied
gives the basic guarantee).

The functions may throw standard exceptions
like std::bad_alloc. Note however that, unfortunately, the standard does not guarantee allocation-failures
in standard containers to be reported by std::bad_alloc or exceptions derived from std::exception.

Note that we pass
a second template argument to list_inserter so argument
lists will be used to construct a V object. Otherwise we
could end up trying to call push_back() with n arguments
instead of one.

An alternative way would be to use boost::function and
boost::bind() in combination. However, in this case one must
remember that it is illegal to take the address of a function in
the standard library.

Calling a function with more that one argument can be
very useful too. This small example shows how we take advantage of this
functionality:

The idea for an assignment/initialization library is not new. The
functionality of this
library resembles Leor Zolman's STL Container Initialization Library a great
deal, but it does not rely on string parsing to achieve its goals.

The
library is non-intrusive and puts only a minimum of requirements
on its supported classes.
Overloading operator comma is sometimes viewed as a bad practice [1]. However, it has been done
with success in eg. the Generative Matrix Computation Library and Blitz to initialize matrices
(see [2]) and [3]). The
Initialization Library overloads
the comma operator in a safe manner by letting free standing functions
return an object that is responsible for the initialization. Therefore it takes
explicit
action from the programmer to begin using the overloaded operator,().

There has recently been some discussion about enhancing the language to
support better initialization (see [4]).

Special thanks goes to

Leor Zolman for our many discussion that eventually led to this library.

Tom Brinkman for being review manager.

Joaquín Muñoz for vc6/vc7 portability.

Pavel Vozenilek for his countless suggestions, improvements and
portability fixes.