当存在用户定义的析构函数时,如何禁用隐式定义的副本构造函数的生成 [英] How to disable implicitly-defined copy constructor generation when there is user defined destructor

查看:224
本文介绍了当存在用户定义的析构函数时,如何禁用隐式定义的副本构造函数的生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何编译器标志来强制执行下一条规则?

Are there any compiler flags to enforce next rules?


如果T具有用户定义的析构函数或用户定义的副本分配操作符。

The generation of the implicitly-defined copy constructor is deprecated if T has a user-defined destructor or user-defined copy assignment operator.

不建议使用隐式定义的副本分配操作符生成
(自C ++ 11起)如果T具有用户声明的析构函数或
用户声明的副本构造函数。

The generation of the implicitly-defined copy assignment operator is deprecated(since C++11) if T has a user-declared destructor or user-declared copy constructor.

我有兴趣执行规则在任何Clang,Visual Studio 2013或GCC中作为代码库都将与它们一起编译。

I'm interested to enforce the rules in any of Clang, Visual Studio 2013 or GCC as the codebase will be compiled with all of them.

推荐答案

错误报告提到了此测试用例,该用例在gcc中不会发出警告:

This bug report mentions this test case which doesn't emit a warning in gcc:

struct W {
  int a;
  ~W() { a = 9; }
};

int main() {
 W w {};
 W v = w;
}

请参考Johnathan Wakely的评论:

Refer to Johnathan Wakely's comment:


事实并非如此,编译器可以(并且确实)警告您合法代码。

That's not true, the compiler can (and does) warn about legal code.

我确认这一点,我们会在某个时候想要警告,并且它
可以使我们改善 -Weffc ++ 警告的这一部分:

I'm confirming this, we will want the warning at some point, and it would allow us to improve this part of the -Weffc++ warnings:

*项目11:为具有动态分配的内存的类定义一个复制构造函数和一个赋值运算符。

* Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory.

(请参阅 PR 16166
了解详情)

(see PR 16166 for more details)

也许我们可以将此警告称为 -Wdeprecated-special-members ,而
启用了此警告 -Weffc ++ 在C ++ 11中, -Wdeprecated

Maybe we could call this warning -Wdeprecated-special-members, and have it enabled -Weffc++ and in C++11 also by -Wdeprecated

Clang已经警告过 -Wpreprecated

Clang already warns about this with -Wdeprecated:

main.cpp:3:3: warning: definition of implicit copy constructor for 'W' is deprecated because it has a user-declared destructor [-Wdeprecated]

  ~W() { a = 9; }

  ^

main.cpp:8:8: note: implicit copy constructor for 'W' first required here

 W v = w;

Microsoft 明确指出在这种情况下Visual Studio将不会发出警告:

Microsoft explicitly states that Visual Studio will not emit a warning in this case:


另外,C ++ 11标准指定了以下其他
规则:

Additionally, the C++11 standard specifies the following additional rules:


  • 如果是复制构造函数或析构函数

  • If a copy constructor or destructor is explicitly declared, then automatic generation of the copy-assignment operator is deprecated.

如果显式声明了复制分配运算符或析构函数,则自动生成

If a copy-assignment operator or destructor is explicitly declared, then automatic generation of the copy constructor is deprecated.

在这两种情况下,Visual Studio都会继续自动生成
必要的功能隐式地,并且不会发出警告。

In both cases, Visual Studio continues to automatically generate the necessary functions implicitly, and does not emit a warning.

这篇关于当存在用户定义的析构函数时,如何禁用隐式定义的副本构造函数的生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆