Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang compilation works while gcc doesn't for diamond inheritance

I'm not sure if I'm missing something here but it seems that the following code (a similar one can be found into another answer I can no longer find, the question here is different by the way) is compiling just fine for clang and not compiling for gcc

#include <iostream>
using namespace std;

class base {
public:
 base(int i) {};

private:
 base(){};
};

class derivedA: virtual public base {
public:
 derivedA(int i) : base(i) {};

protected:
  derivedA() : base(0) {};
};

class derivedB: virtual public base {
public:
 derivedB(int i) : base(i) {};

protected:
  derivedB() : base(0) {};
};

class derivedAB : public derivedA, public derivedB {
public:
 derivedAB(int i) {};
 virtual ~derivedAB() = 0;
};

derivedAB::~derivedAB() {};

class lastDerived : public derivedAB {
public:
    lastDerived() : base(1), derivedAB(0) {};
};

int main(){
        lastDerived obj;
}

gcc is reporting

main.cpp: In constructor 'derivedAB::derivedAB(int)':
main.cpp:9:2: error: 'base::base()' is private
  base(){};

Which one is the correct behavior? I'd say gcc's one but I'm not sure why.

like image 449
Marco A. Avatar asked Jul 02 '14 17:07

Marco A.


1 Answers

A virtual base class of an abstract class does not need to be initialized in the mem-initializer-list of a constructor of that abstract base class.

This is discussed in 12.6.2p8:

[...] and the entity is not a virtual base class of an abstract class [...]
[ Note: An abstract class (10.4) is never a most derived class, thus its constructors never initialize virtual base classes, therefore the corresponding mem-initializers may be omitted. — end note ]

So clang is correct, and gcc is incorrect. This would be different were derivedAB not abstract.


This is a new allowance in C++11, since DR 257; g++ is correct for C++03. There is a gcc bug at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19249; perhaps it could do with poking.

like image 98
ecatmur Avatar answered Oct 03 '22 18:10

ecatmur