C++的核心准则包含以下建议关于virtual,override和final说明符,具体涉及派生类析构函数:
如果基类析构函数声明为virtual,则应避免声明派生类析构函数为virtual或override.一些代码库和工具可能会坚持覆盖析构函数,但这不是这些指南的建议.
果然,clang-tidy这是违反建议的工具之一.如果我没有指定virtual或者override,则运行clang-tidy会发出以下警告:
warning: annotate this function with 'override' or (rarely) 'final' [modernize-use-override]
Run Code Online (Sandbox Code Playgroud)
或者,如果指定如下virtual:
warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override]
Run Code Online (Sandbox Code Playgroud)
鉴于override特别指定具有(至少)确定基类析构函数的优点virtual,我的问题归结为以下特定部分:
virtual或override派生类析构函数的参数是什么?clang-tidy建议,还是倾向于C++核心指南建议?我有一个继承自的异常类的实现std::runtime_error:
class custom_error : public std::runtime_error {
public:
explicit custom_error(const std::string &arg)
: std::runtime_error(arg){};
~custom_error() noexcept override = default;
};
Run Code Online (Sandbox Code Playgroud)
该实现尽可能与libstdc++其他子类的实现相同std::runtime_error.
当对此代码运行铿锵(在某些情况下 - 见下文)时,会发出以下警告:
warning: thrown exception type is not nothrow copy constructible [cert-err60-cpp]
Run Code Online (Sandbox Code Playgroud)
有关此检查的相关信息,请参见此处.我相信我理解该页面上的信息.特别是,我认为相关信息是a std::runtime_error及其子类应具有的特征is_nothrow_copy_constructible.
我无法协调以下内容.
编译器资源管理器上的以下代码演示了is_nothrow_copy_constructible使用gcc 5.x和更新版本编译时的类,但在使用gcc 4.x编译时则不行.
在我的笔记本电脑上,运行Ubuntu 18.04,同样的类也在is_nothrow_copy_constructible编译时使用gcc 5,gcc 7,clang 3.8,clang 5和clang 6,所有这些都是在链接时libstdc++.so.6.0.25.
在运行Ubuntu 14.04实例的travis-ci上,我找不到同一个类的单个配置is_nothrow_copy_constructible.这包括使用gcc 5,gcc 6,gcc 7,clang 4和clang 5,链接libstdc++.so.6.0.25.
这是一个 …