Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding -Weffc++

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:

  1. 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?

  2. 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 no delete. 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?

like image 551
chris Avatar asked Jul 16 '12 00:07

chris


People also ask

Whats does understanding mean?

To be understanding is to be sympathetic to someone's woes. Understanding a concept means you get it. Your understanding might be that your mother will always drive you to school if you miss the bus. The sum of your knowledge of a certain topic, is your understanding of it. This can change, or deepen as you learn more.

What is another word for being understanding?

The words appreciate and comprehend are common synonyms of understand. While all three words mean "to have a clear or complete idea of," understand and comprehend are very often interchangeable, with understand sometimes stressing the fact of having attained a firm mental grasp of something.

What is understanding in life?

It means that you're sympathetic to what someone is going through and you're trying to both feel and see things from their point of view. When someone is understanding, they also find it easy to connect with people as it's in their nature to do so.

What is the use of understanding?

Understanding implies abilities and dispositions with respect to an object of knowledge that are sufficient to support intelligent behavior. Understanding is often, though not always, related to learning concepts, and sometimes also the theory or theories associated with those concepts.


2 Answers

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!

like image 163
Jonathan Wakely Avatar answered Sep 27 '22 22:09

Jonathan Wakely


  1. When you do it, you have a POD structure. As it cannot have any constructors, -Weffc++ doesn't bother checking.

  2. Use a reference or shared_ptr object or any other object that wrap a pointer.

like image 20
Arpegius Avatar answered Sep 27 '22 20:09

Arpegius