Search code examples
c++g++abstract-class

Does GCC11 have a regression in which it incorrectly accept pointer to array of abstract class


In GCC 4 to 10 and Clang (all versions), this piece of code does not compile because the compiler is unable to allocate memory for A because A is abstract:

class A {
    virtual void f() = 0;
};

void f(A (*)[1]) {}

However, in GCC11+ this appears to compile successfully.

This idiom is useful for detecting an abstract class before C++11 and Boost indeed uses this for their type_traits library.

Did I miss something or is this a regression in the compiler?


Solution

  • P0929R2 changed the rules so that it's no longer ill-formed to mention the type "array of X" where X is an abstract class type. (Of course, such a type can never be instantiated.) This change was made because it was sort of silly that you were allowed to mention the "array of X" type when X is incomplete, only to later define X as an abstract class type and thus trigger a compilation error at the point of definition.

    Because this change was approved as a Defect Report in the Rapperswil meeting (June 2018), newer versions of compilers should apply the new rules in all language versions (which in this case means all the way back to C++98).