Re: [eigen] Question about the class hierarchy |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Question about the class hierarchy
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Wed, 30 Jun 2010 11:21:44 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=fxAzemHy7uAP+YeSeE1Ci085RptT6u9mT4mkMKlkXgU=; b=DeJPFE3jRVliD+CMa2LBC+61sdGcpw+ZOKkRPCDdmBne+PpAJEmhORmZOjwpjrPD8c mDpY3/ItdkPAGSvSjMfzRLZ0yI1sh1OEwwT/EIYv6WhXozOTuqAvSXRCpGA8J8xqzWlu m50xegLk0+ecz52asuw4sndhVcNOIw3wpqk80=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=KJ0PTYDXzOj5OjkL/fV7a6XpM+tAMKBQKjG557/MLQc9xf1SK0bi5Do56LqQy1PV7A 6Gf7WH/dDGPLUktY9o+44MFYsYnkfcwsDbvUB0oI26ESGLFk8M1zP6JIPxyH66rb8xYS 5RGgtRTz79FxS1Xd4BuS3mib5ZykSgycOB5qI=
2010/6/30 Manoj Rajagopalan <rmanoj@xxxxxxxxx>:
>
>
>
> On Wednesday 30 June 2010 04:28:04 am Hauke Heibel wrote:
>> We try hard to prevent multiple-inheritance since we have encountered
>> examples where MSVC fails to perform empty base class optimization
>> which is crucial for our fixed size types.
>
> ==== Ouch! So microsoft had to continue getting in the way ...
>
>>
>> > 3. If the Array base and Matrix base must inherit EigenBase then this
>> > inheritance must be virtual so that there is exactly one copy of
>> > EigenBase in the derived object. virtual inheritance works with templates
>> > at least with gcc 4.2 on my linux machine.
>>
>> I don't quite understand this? The EigenBase has zero size. No
>> members, no virtual functions. Having even a single virtual function
>> in EigenBase is not an option because of the sizeof constraint I told
>> you about before.
>>
>
> Oh, I didn't mean virtual functions - I just meant using the "virtual" keyword
> in the inheritance spec just following the class name in its declaration.
> This is explained here:
> http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8
>
> In Eigen's case, EigenBase is at the top of the dreaded diamond. ArrayBase and
> MatrixBase both inherit it. If we hypothetically consider the simplified
> multiple-inheritance picture of a dense matrix that I enquired about, then
> these declarations would look like:
>
> class ArrayBase : public virtual EigenBase {...};
> class MatrixBase : public virtual EigenBase {...};
>
> Then (dense) Matrix would inherit both the usual way, without the "virtual"
> keyword. The "virtual" in the above decls ensures that only one copy of
> EigenBase is made in Matrix but the latter's CTOR will have to call the
> EigenBase CTOR directly even though the inheritance is indirect.
>
> But virtual inheritance does create space overheads
Yes, that's why we don't use virtual at all in Eigen's class
hierarchy. Instead, we do compile-time polymorphism: the base class
takes the derived class as template parameter, so that it knows which
particular derived class it is the base of. Different derived class
types have different base class types. For example:
template<typename Derived>
class MatrixBase
This is known as the CRTP,
http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
Benoit
> so it may not be suitable
> for the reason you mentioned even if there are no virtual functions. I am
> attaching an experiment program with the dreaded diamond inheritance. the top
> base class just wraps in int. The sizeof() outputs show that 4 bytes are
> added with each layer of virtual inheritance but this is not so with regular
> inheritance.
>
> With virtual inheritance:
> sizeof(int) = 4
> sizeof(A<int>) = 4
> sizeof(B<int>) = 8
> sizeof(C<int>) = 8
> sizeof(D<int>) = 12
>
>
> Without vitual inheritance:
> sizeof(int) = 4
> sizeof(A<int>) = 4
> sizeof(B<int>) = 4
> sizeof(C<int>) = 4
> sizeof(D<int>) = 8
>
>
> Thanks,
> Manoj
>