|
Boost : |
Subject: Re: [boost] [exception] Virtual inheritance with no default constructors
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-05-13 02:44:53
On Tue, May 12, 2009 at 12:21 AM, Adam Badura <abadura_at_[hidden]> wrote:
> The Boost.Exception library mandates that exceptions classes hierarchy
> should use virtual inheritance
> <snip>
> 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.
Good point.
Definitely, the documentation needs to be updated to note the problem.
In general, a good guideline to follow when using virtual inheritance
is to always provide a default constructor for the virtual bases.
I came up with a solution which would involve renaming
boost::exception_detail::clone_base to boost::exception_clone_base,
documenting its requirements, and modifying
boost::enable_current_exception to be able to detect if the type of
its argument derives from boost::exception_clone_base. In that case,
it wouldn't wrap it, and thus it is up to the user-defined exception
type to initialize any virtual bases, including the ones that can't be
default-initialized.
See the attached patch -- I've only tested with MSVC but it should
work with other compilers too. I'm not committing this to trunk yet.
It seems acceptable, but I'm not sure if this extra complication is
worth the trouble. I doubt that user code that requires a similar
workaround exists in practice.
Was your observation mostly theoretical, or was it based on trouble
you had with existing code?
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk