Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2001-07-09 20:31:08


On Monday 09 July 2001 06:58, you wrote:
> I'm playing with the boost:: function library for the first time, probably
> a naive question but, could someone explain to me why the mixins and
> policies classes are expected to be stateless i.e. always default
> constructed?
>
> Thanks,
>
> Richard
>
> COINCIDENCE: You weren't paying attention to the other half of what was
> going on. - The Hipcrime Vocab by Chad C. Mulligan.

Always default constructed and stateless are two different things.

Mixins add user-defined state. They are default-constructed because this
appears to be common practice for mixins; however, it would be a small matter
to include the ability for the user to specify an initial value.

Policy classes aren't entirely stateless. They retain state over one
invocation of a Boost.Function object (i.e., the same policy object will have
its precall and postcall methods invoked for a single invocation of the
Boost.Function object), but they do not retain state between invocations.
Policies are default-constructed because there doesn't appear to be any
useful information to pass to the policy constructor.

An alternative to the policy precall/postcall methods was to construct the
policy given a pointer to the Boost.Function object (that would serve as the
"precall") and the destruction of the policy object would be equivalent to
"postcall." This approach is reasonable, although there may be problems with
exception safety. While the precall/postcall member functions can safely
throw, this isn't necessarily true of the constructor/destructor policy type.
It's safer not to take that risk...

Policies don't keep state between invocations because that would cause in
increase in the size of Boost.Function objects, even for empty policies.

Mixins won't increase the size of Boost.Function objects because of the empty
base class optimization.

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk