Boost logo

Boost :

Subject: [boost] [exception] Virtual inheritance with no default constructors
From: Adam Badura (abadura_at_[hidden])
Date: 2009-05-12 03:21:24


The Boost.Exception library mandates that exceptions classes hierarchy
should use virtual inheritance
(http://www.boost.org/doc/libs/1_39_0/libs/exception/doc/using_virtual_inheritance_in_exception_types.html).
The document notes that this does not go well with exceptions without
default constructors ("(...) Note that virtual bases are initialized
directly by the constructor of the most-derived-type (...)") however this
should not be a concern as boost::exception provides other means to
transport data.

What is missing is however documentation that in normal circumstances
boost::throw_exception cannot be used with exceptions types which have in
their inheritance graph virtual base classes which are not default
constructible.

This is because boost::enable_current_exception constructs the clone_impl
object which inherits the actual exception type and copy-constructs it.
However this code will not compile if some base class of the callers
exception type is virtual and non-default constructible.

In my opinion few changes seem appropriate:

1) Note in the documentation explicitly this requirement (possibility of
default construction).

2) Name an entity which should be made friend of exception class to use its
non-public default constructor. This entity would be latter used by
clone_impl. This way an exception could be default constructible only for
this particular use case.

3) Provide means to late-initialize exception object. This would bypass the
default construction problem (and goes well with private default constructor
as described in 2)).

4) Allow to specify in boost::throw_exception (or similar places) not only
the exception type but as well its (virtual) base types so that latter
clone_impl could mention them on the initializers list as well and
copy-construct them. (As far as I know there is no way of automatic
detection of base types thus caller must specify them explicitly - however
if I am mistaken such automation would be even better.)

Adam Badura


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