英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论