C ++ 11 class lattice with mixed virtual and non-virtual bases?

C ++ 11 (N3485) 10.1.4 [class.mi] says:

For each separate occurrence of a non-virtual base class in the class lattice of the most derived class, the most derived object must contain the corresponding separate subobject of the base class of this type.

For each individual base class that is specified as virtual, the most derived class must contain one base class object of this type.

Consider the following C ++ 11 code:

struct B {}; struct BV : virtual B {}; struct BN : B {}; struct C1 : BV, BN {}; struct C2 : BV, BN {}; struct D : C1, C2 {}; 

First, for clarity, how many vertices does a class D lattice have?

Secondly, how many different subobjects of type B usually require that a derived object of type D itself have?

update:

Which of the following class lattice?

(one)

  BBBB ^ ^ ^ ^ | | | | BV BN BV BN ^ ^ ^ ^ | | | | \ / \ / C1 C2 \ / \ / - D - 

(2)

  B<--------- ^ \ | | | B | B | ^ | ^ | | | | BV BN BV BN ^ ^ ^ ^ | | | | \ / \ / C1 C2 \ / \ / - D - 

(3)

  B / \ / \ BV BN | \ / | | \/ | | / \ | | / \| C1 C2 \ / \ / D 

If the intention is that it is (1), then is it not impossible to have any DAG that is not a tree? (i.e. diamond is not possible). If it would be better to call it a class tree?

If this is (2), it would not be enough to say "for each appearance of the base class in the class lattice there is a corresponding subobject of the base class". That is, if the construction of the lattice already depends on the relationship of the virtual and non-virtual base classes for selecting edges and vertices?

If this is (3), then this is not the language that is incorrect in the standard, because there can be only one appearance of a class in the class grid?

+7
source share
1 answer

Which of the following class lattice?

2

Demonstration:

 #include <iostream> struct B {}; struct BV : virtual B {}; struct BN : B {}; struct C1 : BV, BN {}; struct C2 : BV, BN {}; struct D : C1, C2 {}; int main() { D d; C1* c1 = static_cast<C1*>(&d); BV* bv1 = static_cast<BV*>(c1); BN* bn1 = static_cast<BN*>(c1); B* b1 = static_cast<B*>(bv1); B* b2 = static_cast<B*>(bn1); C2* c2 = static_cast<C2*>(&d); BV* bv2 = static_cast<BV*>(c2); BN* bn2 = static_cast<BN*>(c2); B* b3 = static_cast<B*>(bv2); B* b4 = static_cast<B*>(bn2); std::cout << "d = " << &d << '\n'; std::cout << "c1 = " << c1 << '\n'; std::cout << "c2 = " << c2 << '\n'; std::cout << "bv1 = " << bv1 << '\n'; std::cout << "bv2 = " << bv2 << '\n'; std::cout << "bn1 = " << bn1 << '\n'; std::cout << "bn2 = " << bn2 << '\n'; std::cout << "b1 = " << b1 << '\n'; std::cout << "b2 = " << b2 << '\n'; std::cout << "b3 = " << b3 << '\n'; std::cout << "b4 = " << b4 << '\n'; } 

My conclusion:

 d = 0x7fff5ca18998 c1 = 0x7fff5ca18998 c2 = 0x7fff5ca189a0 bv1 = 0x7fff5ca18998 bv2 = 0x7fff5ca189a0 bn1 = 0x7fff5ca18998 bn2 = 0x7fff5ca189a0 b1 = 0x7fff5ca189a8 b2 = 0x7fff5ca18998 b3 = 0x7fff5ca189a8 b4 = 0x7fff5ca189a0 

If this is (2), it would not be enough to say "for every occurrence of a base class in the class lattice there is a corresponding base class subobject", i.e. if the construction of the lattice already depends on the relationship of the virtual and non-virtual base classes for selection by edges and vertices?

Merging your offer ...

The base class specifier containing the virtual keyword defines the virtual base class. For each a separate base class appearance non-virtual in class lattice the most derived class, the most derived object (1.8) should contain , there is a corresponding separate base class subobject of this type . For each individual base class that is specified as virtual, the most derived object must contain one subobject of a base class of this type.

I am not a half standard language expert. However, when I read the modified specification, I do not see how:

 class V { /βˆ—...βˆ—/ }; class A : virtual public V { /βˆ— ... βˆ—/ }; class B : virtual public V { /βˆ— ... βˆ—/ }; class C : public A, public B { /βˆ—...βˆ—/ }; 

The results in Figure 4:

  V / \ / \ AB \ / \ / C 

I see no other place in the standard that indicates that although V appears twice in the class hierarchy below C , only one subobject of type V actually exists due to the use of the virtual keyword.

+3
source

All Articles