GCC11是否存在一个错误,它错误地接受指向抽象类数组的指针?

huangapple go评论52阅读模式
英文:

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

问题

在GCC 4到10和Clang(所有版本)中,这段代码无法编译,因为编译器无法为A分配内存,因为A是抽象的:

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

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

然而,在GCC11+中,这似乎可以成功编译。

在C++11之前,这种习惯用法用于检测抽象类,Boost确实在其type_traits库中使用了这种方法。

我有遗漏什么吗,还是这是编译器的一个退化?

英文:

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?

答案1

得分: 3

P0929R2 更改了规则,不再将提到类型“数组X”视为不合法,其中X是抽象类类型。(当然,这样的类型永远无法实例化。)之所以做出这一更改,是因为允许在X不完全的情况下提到“数组X”类型,然后稍后将X定义为抽象类类型,从而在定义点触发编译错误,这似乎有点荒谬。

由于这一更改已经在Rapperswil会议(2018年6月)中被批准为缺陷报告,因此新版本的编译器应该在所有语言版本中应用新规则(在这种情况下,这意味着一直适用到C++98)。

英文:

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).

huangapple
  • 本文由 发表于 2023年2月16日 02:43:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定