|
Boost : |
Subject: Re: [boost] [xint] Boost.XInt formal review
From: Gordon Woodhull (gordon_at_[hidden])
Date: 2011-03-05 14:17:07
On Mar 5, 2011, at 1:53 PM, Chad Nelson wrote:
>
>> in the end i have a question to chad: for what reason integer_t
>> inherits its implementation *virtualy* ?
>> you know that virtual inheritance involves (possibly unneeded)
>> overhead
>
> Because many (all?) of the classes it inherits from have to have access
> to the same data, so they all inherit from the same base class. The
> "virtual" there tells the compiler that all of those inheritances come
> from a single instance of the base class, rather than a separate one in
> every case.
>
> I believe this use of virtual results only in compile-time overhead.
> Logically there shouldn't be any need for the run-time code to even
> notice that virtual was used there. But there may be more going on
> behind the scenes than I'm aware of.
Virtual inheritance is designed for the general case where classes don't know what hierarchy they are part of, and so they need to lookup an offset in a dynamic table (or in older implementations, dereference a pointer). In your case, all your classes can know what the most derived class is, so a cheap static_cast to derived will do.
With your design, it is possible (though unlikely) that someone else would derive from integer_t with their own virtual inheritance from integer_t_data, thus moving around the location of the virtual base. That's why the compiler can't optimize this away, AFAIK.
There's a fun section on this in Lippman "Inside the C++ Object Model" pp.95-101.
(Yes, I use this as an interview question.)
Cheers,
Gordon
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk