Pergunta

3Trying to compile the code below with ICC return this error: error #453: protected function "A::A()" (declared at line 10) is not accessible through a "A" pointer or object.

class A
{
protected:
    constexpr A() = default;
    ~A() = default;

    A(const A&) = delete;
};

class B
    : protected A
{
public:
    B() = default;
};

int main()
{
    B b;
}

I found 3 weird ways to make it compilable:

  • making the ctor of A public
  • removing the deleted copy ctor of A
  • replacing "= default;" by "{}" in the ctor of A

I mean, why the h.. ?

Thank you for your answers :)

Foi útil?

Solução

I confirm this misbehaviour on v13.1.3 (Linux). This is certainly a compiler bug as Arne Mertz says: I find that if A is simply provided with an otherwise pointless data member that is initialized at declaration, the class compiles, e.g.

class A
{
protected:
    constexpr A() = default;
    ~A() = default;

    A(const A&) = delete;

private:
    char placate_intel_compiler_bug = 0;
};

I don't know what compiler version you have, so don't know if it supports non-static data member initialization (or if the same fix would work for you), but if it does then this is a 5th workaround you might consider for the merit that its intent is unmistakable.

Removing constexpr has no effect on the bug.

Of the 3 workarounds you found the third one, replacing the defaulted A::A() with an explicit A::A(){} is the only one that does not mess with the desired public behaviour of your class.

Arne Mertz's 3b also works for me but that one has the downside of putting the solution outside class A.

If in the real world you have a particular reason for declaring A::A() as constexpr then bear in mind, for the 3rd workaround, that if a constexpr constructor is not default-ed then the C++11 Standard § 7.1.5, para 4, places rather fiddly constraints on the constructor and its class that could make your code more fragile in maintenance. Another possible plus for the in-your-face 5th workaround.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top