Inheritance of constructors of virtual base classes

Virtual base classes are initialized in the derived class itself, so I assume that inheriting the base class constructor should also work:

struct base { base(int) {} }; struct derived: virtual base { using base::base; }; derived d(0); 

However, this is not compiled with GCC 5.2.0, which tries to find base::base() , but works fine with Clang 3.6.2. Is this a bug in GCC?

+5
source share
1 answer

This is gcc 58751 error "[C ++ 11] Inheritance of constructors does not work properly with virtual inheritance" (aka: 63339 "using constructors" from virtual databases is implicitly removed "):

From the description of 58751:

Document N2540 states that:

As a rule, inheritance of design definitions for classes with virtual databases will be poorly formed, unless the virtual database supports initialization by default, or the virtual database is a direct database and is called a redirected database. Similarly, all data members and other direct databases must support initialization by default, or any attempt to use the inheriting constructor will be poorly formed. Note: poorly formed when used, not announced.

Therefore, the case of virtual databases is clearly considered by the committee and, therefore, should be implemented.

Workaround borrowed from bug report:

 struct base { base() = default; // <--- add this base(int) {} }; 

According to the error report, in this case, the constructor base::base(int) is called by the implicitly created constructor derived::derived(int) .

I checked that your code is not compiling. But this does and calls the constructor base::base(int) .

+5
source

Source: https://habr.com/ru/post/1233037/


All Articles