kim366 kim366 -4 years ago 78
C++ Question

C3881 in Visual Studio, while it compiles without any problems in g++

struct Base
{
};

struct DerOne : Base
{
};

struct DerTwo : Base
{
};

struct Joined : DerOne, DerTwo
{
using Base::Base;
};

int main()
{
Joined j;
return 0;
}


produces


Error C3881 can only inherit constructor from direct base


in Visual Studio, while it works fine in g++. Is this code correct?

Answer Source

This is a GCC bug. What it's doing cannot work, even without checking the standard.

#include <stdio.h>

struct Base {
    Base() { puts("Base()"); }
    Base(int) { puts("Base(int)"); }
};

struct DerOne : Base { };

struct DerTwo : Base { };

struct Joined : DerOne, DerTwo {
    using Base::Base;
};

int main() {
    Joined j {4};
}

With GCC, this allows a Joined(int) constructor to be created from the Base(int) signature, but it doesn't call the Base(int) implementation (and cannot, because there are no DerOne or DerTwo constructors which would call it), so this ends up printing Base() twice.

clang rejects it just like Visual Studio does.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download