理解-Weffc++
考虑以下程序:
#include <string>结构 S {S (){}私人的:无效 *ptr = nullptr;std::string str = "";};int main(){}
当在 GCC 4.7.1 上使用 -Weffc++
编译时,会吐出:
这通常没问题,除了这个例子中的几件事:
如果我注释掉任何构造函数、指针声明或字符串声明,警告就会消失.这很奇怪,因为您认为仅指针就足够了,但事实并非如此.此外,将字符串声明更改为整数声明也会导致它消失,因此它仅在存在字符串(或可能其他选择类)时出现.为什么在这些情况下警告会消失?
当所有指针都指向一个现有变量(通常由操作系统维护)时,通常会出现此警告.没有
new
,也没有delete
.在这些情况下,当复制带有句柄的类时,我不想要深拷贝.我希望两个句柄都指向同一个内部对象(例如窗口).有没有办法让编译器实现这一点,而无需不必要地重载复制构造函数和赋值运算符,或者使用#pragma
完全禁用警告?为什么我一开始就在三法则都不适用的情况下受到困扰?
GCC的-Weffc++
有几个问题,我没用过.检查问题"的代码非常简单,因此警告最终过于生硬和无用.
该特定警告基于Effective C++第一版的第 11 项,Scott 在以后的版本中对其进行了更改(为了更好).G++ 代码不检查实际的动态分配,只检查指针成员的存在.
查看我在 GCC 的 bugzilla 中写的关于此警告的内容在比较第一版和第三版的指南时:
<块引用><块引用>第 11 项:为类定义一个复制构造函数和一个赋值运算符动态分配内存.
替换为第 14 条:仔细考虑复制行为资源管理类"――建议不那么具体,但更有用.我不知道如何把它变成一个警告!
Consider the following program:
#include <string>
struct S {
S (){}
private:
void *ptr = nullptr;
std::string str = "";
};
int main(){}
This, when compiled with -Weffc++
on GCC 4.7.1, will spit out:
warning: 'struct S' has pointer data members [-Weffc++] warning: but does not override 'S(const S&)' [-Weffc++] warning: or 'operator=(const S&)' [-Weffc++]
That's no problem normally, except for a couple things with this example:
If I comment out any of the constructor, the pointer declaration, or the string declaration, the warning disappears. This is odd because you'd think the pointer alone would be enough, but it isn't. Furthermore, changing the string declaration to an integer declaration causes it to disappear as well, so it only comes up when there's a string (or probably other choice classes) with it. Why does the warning disappear under these circumstances?
Often times this warning comes up when all the pointer is doing is pointing to an existing variable (most often maintained by the OS). There's no
new
, and nodelete
. When the class with the handle, in these cases, is copied, I don't want a deep copy. I want both handles to point to the same internal object (like a window, for example). Is there any way to make the compiler realize this without unnecessarily overloading the copy constructor and assignment operator, or disabling the warning completely with#pragma
? Why am I being bothered in the first place when the Rule of Three doesn't even apply?
解决方案
GCC's -Weffc++
has several issues, I never use it. The code that checks for "problems" is pretty simplistic and so the warnings end up being far too blunt and unhelpful.
That particular warning is based on Item 11 of the first edition of Effective C++ and Scott changed it (for the better) in later editions. The G++ code doesn't check for actual dynamic allocation, just the presence of pointer members.
See what I wrote about this warning in GCC's bugzilla when comparing the guidelines in the first edition with the third edition:
Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory.
Replaced by Item 14: "Think carefully about copying behavior in resource-managing classes" - the advice is less specific, but more useful. I'm not sure how to turn it into a warning though!
相关文章