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

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


On 15 Aug 2011, at 10:14, Eli Friedman 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

How about this wording:

    constructor for 'Derived' must explicitly initialize the base class 'Base'
    because it does not have a default constructor

and

    constructor for 'Derived' must explicitly initialize the base class 'Base'
    because it is a virtual base and does not have a default constructor

--David Röthlisberger.





More information about the cfe-commits mailing list