gpt4 book ai didi

c++ - 强制 clang 在此 C++ 代码片段上生成编译错误

转载 作者:行者123 更新时间:2023-12-03 06:53:43 25 4
gpt4 key购买 nike

我有这个无效代码:

class X {
public:
virtual void func() = 0;
};

class JustDeclared {
X missing();
};


int main() { return 0; }

它不能在 gcc 下编译,因为它不可能创建抽象类 X 的实例,而这恰恰已被声明为 JustDeclared::missing( ) 方法。

但是 clang++ 和 Visual Studio 编译这段代码没有问题。我怀疑这是因为在实际编译之前简单地剥离了 AST 中未使用的部分。

但是是否有可能启用一些“迂腐”模式来强制 clang 为该代码段生成错误?

最佳答案

直到 C++14,在 C++17 的原始版本中,这被认为是 Clang 和 MSVC 中的一个错误,对于 Clang(见下文)和 MSVC 可能是因为正在执行抽象类类型诊断在函数定义时而不是在(第一次)函数声明时(与 ISO 标准规则相反)。如 this related Q&A which branched off from this question 所述, 但是,从 C++20 和 P0929R2 开始Clang 和 MSVC 实际上接受该程序是正确的,而 GCC 拒绝它是错误的。如 P0636R3 中所述, P0929R2 应被视为 C++17 中的缺陷报告。


以下所有标准引用均引用 N4659: March 2017 post-Kona working draft/C++17 DIS .

C++17 之前的标准行为

根据 [class.abstract]/3 [强调我的]

An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion.Pointers and references to an abstract class can be declared.[ Example:

shape x;           // error: object of abstract class
shape* p; // OK
shape f(); // error
void g(shape); // error
shape& h(shape&); // OK

 — end example ]

抽象类不能用作函数返回类型,因此即使未使用 JustDeclared,您的程序也是病式的。

对于 Clang,这是错误报告

自 2016 年以来一直处于休眠状态。2014 年提交了修复错误的尝试:

它确定了与上面引用的标准相同的不合规性[强调我的]:

...

Attached patch contains the implementation of a fix for PR18393[1]. According to standard "An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion" (class.abstract $10.4.3).

Currently, checking if type isn't abstract class is done when methodis defined, but I don't see any reason why clang shouldn't do it asearly as possible, in this case, when function/method is declared.Test also attached.

...

然而,该补丁尚未完成,可以说似乎已经死了。


But is it possible to enable some "pedantic" mode that will force clang to generate an error for this snippet?

如上所述,对于 C++20 和 C++17(缺陷向后移植)Clang 实际上是正确的接受程序。因此,即使在 C++14(以及未移植缺陷的 C++17)之前这是一个错误,上面的错误报告也可能会被弃用,因为从 P0929R2 开始,标准已更改为以前是 Clang 的错误行为。

请注意,如果您不仅声明而且还定义了 JustDeclared::missing(),Clang 将通过违反 [class.abstract]/3 正确地将程序诊断为格式错误,这也适用于 C++20(/应用 P0929R2 之后)。

关于c++ - 强制 clang 在此 C++ 代码片段上生成编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63950540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com