[英]g++ [[noreturn]] on a virtual method
I've run into some trouble trying to clean up g++ compiler warnings.我在尝试清理 g++ 编译器警告时遇到了一些麻烦。
Say I have this class:假设我有这个 class:
class A
{
public:
[[noreturn]] virtual void will_throw() { throw 0; }
};
And inside a non-void function I invoke will_throw
without returning.在非 void function 中,我调用
will_throw
而不返回。
If I do this by value, ie:如果我按价值这样做,即:
int g()
{
A a;
a.will_throw();
}
then I get no -Wreturn-type
warnings.然后我没有
-Wreturn-type
警告。
If I do it by pointer:如果我通过指针来做:
int g()
{
A a;
A* aptr = &a;
aptr->will_throw();
}
Then I get "warning: no return statement in function returning non-void [-Wreturn-type]"然后我得到“警告:function 中没有返回语句返回非 void [-Wreturn-type]”
If I remove virtual
from the declaration of A::will_throw
then calling it on a pointer also produces no warnings.如果我从
A::will_throw
的声明中删除virtual
然后在指针上调用它也不会产生任何警告。 Calling the method on a reference seems to produce a warning if the method is pure-virtual, but not otherwise.如果该方法是纯虚拟的,则在引用上调用该方法似乎会产生警告,否则不会。
I wasn't able to find anything saying this is how it's supposed to work, and none of these cases produce warnings in Clang.我找不到任何说明它应该如何工作的东西,并且这些情况都不会在 Clang 中产生警告。 Is this a bug in GCC?
这是 GCC 中的错误吗?
Since the function is virtual, the compiler doesn't know (without tracking assignments) that the call through a pointer (or reference) is to A::will_throw
and not to some overriding function that might not be noreturn
.由于 function 是虚拟的,因此编译器不知道(不跟踪分配)通过指针(或引用)的调用是对
A::will_throw
而不是对某些可能不是noreturn
的覆盖 function 的调用。 Since it's just a warning, both behaviors (or never warning, or always warning.) are conforming.由于这只是一个警告,因此两种行为(或从不警告,或始终警告)都符合要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.