Base class constructor second class inheritance: silent error

Today I came across a very nasty bug, here is the MWE:

#include <iostream> class X { public: X() { std::cout << "Default" << std::endl; } X(int a) { std::cout << a << std::endl; } }; class Y : public X { }; class Z : public Y { using X::X; }; int main() { Z instance{3}; } 

Contrary to my expectations, " Default " is printed. Of course, the code is faulty because the inherited constructors of Z try to initialize X without specifying how to construct Y (*) . But still, shouldn't the compiler complain? What is the rationale for the default constructor, Y (and subsequently X ), being invoked completely ignoring my parameter 3 ? Is this documented somewhere in the standard? Or is this a bug in my compiler?

My environment is gcc version 6.2.1 20160916 (Red Hat 6.2.1-2) . A compiler warning is not generated even with -Weffc++ -Wall -Wextra -pedantic .

+8
c ++ gcc inheritance constructor
source share
1 answer

This is a g ++ error, the code is invalid. Only direct database constructors are inherited:

[namespace.udecl] Β§3 If such a use-declaration is called by the constructor, the sub-name specifier must indicate the direct base class of the class being defined

+2
source share

All Articles