[cfe-commits] [PATCH] Helpful message for uninitialized virtual base

David Röthlisberger david at rothlis.net
Mon Aug 15 03:20:11 PDT 2011


On 15 Aug 2011, at 10:16, Sean Hunt wrote:

> On Mon, Aug 15, 2011 at 02:14, Eli Friedman <eli.friedman at gmail.com> wrote:
> On Mon, Aug 15, 2011 at 1:31 AM, David Röthlisberger <david at rothlis.net> wrote:
> > In C++ when you have the following inheritance hierarchy:
> >
> >    Base <--(virtual)-- Middle <---- Derived
> >
> > if Base has no default constructor, then the most-derived class must explicitly call the virtual base's constructor.
> >
> > Clang's current error message is:
> >
> >    constructor for 'Derived' must explicitly initialize the base class 'Base'
> >    which does not have a default constructor
> >
> > Maybe I'm a bit slow, but when I encountered this error I spent about an hour trying to figure out why my initialization of Base in Middle's constructor wasn't being found. This patch adds the following note:
> >
> >    note: because 'Base' is a virtual base, the most-derived class must explicitly
> >    initialize it
> >
> > Is this a good idea? Unnecessary? Is it really clang's job to educate the user on the more obscure aspects of C++? (I would argue that yes, we need all the help we can get.)
> 
> I think a more subtle approach might be more appropriate: we could
> change to existing diagnostic to explicitly say "virtual base class"
> instead of just "base class".  Hopefully, that would be enough to
> point the user in the right direction.
> 
> -Eli
> 
> I can't think of any reason we shouldn't have a note showing where it is virtual.
> 
> Sean

My patch doesn't actually include the source location of where the virtual inheritance happens, but if people think it's a good idea I'll have a stab at it.

--David Röthlisberger.






More information about the cfe-commits mailing list