Understanding -Weffc++

前端 未结 2 823
南旧
南旧 2020-12-03 04:07

Consider the following program:

#include 

struct S {
    S (){}

private:
    void *ptr = nullptr;
    std::string str = \"\";
};

int main(){         


        
相关标签:
2条回答
  • 2020-12-03 04:14
    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.

    0 讨论(0)
  • 2020-12-03 04:27

    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!

    0 讨论(0)
提交回复
热议问题