How to use noncopyable?
Hi, I can't figure out how to properly use noncopyable. I'm always getting things like "has a non-virtual destructor" warnings. In fact, compiling noncopyable_test.cpp gives me: $ g++ -Wall -g3 -o noncopyable_test noncopyable_test.cpp -Wextra -Wconversion -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Winit-self -Wno-unused-parameter -Wparentheses -Wmissing-format-attribute -Wfloat-equal -Winline -Woverloaded-virtual -Wsign-promo -Wc++0x-compat -Wsynth /opt/armed/include/boost/noncopyable.hpp: In copy constructor ‘<unnamed>::DontTreadOnMe::DontTreadOnMe(const<unnamed>::DontTreadOnMe&)’: /opt/armed/include/boost/noncopyable.hpp:27: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ is private noncopyable_test.cpp:22: error: within this context noncopyable_test.cpp: In function ‘int main()’: noncopyable_test.cpp:32: note: synthesized method ‘<unnamed>::DontTreadOnMe::DontTreadOnMe(const<unnamed>::DontTreadOnMe&)’ first required here /opt/armed/include/boost/noncopyable.hpp: In member function ‘<unnamed>::DontTreadOnMe&<unnamed>::DontTreadOnMe::operator=(const<unnamed>::DontTreadOnMe&)’: /opt/armed/include/boost/noncopyable.hpp:28: error: ‘const boost::noncopyable_::noncopyable& boost::noncopyable_::noncopyable::operator=(const boost::noncopyable_::noncopyable&)’ is private noncopyable_test.cpp:22: error: within this context noncopyable_test.cpp: In function ‘int main()’: noncopyable_test.cpp:33: note: synthesized method ‘<unnamed>::DontTreadOnMe&<unnamed>::DontTreadOnMe::operator=(const<unnamed>::DontTreadOnMe&)’ first required here Any hints? Thanks in advance. -- Yang Zhang http://www.mit.edu/~y_z/
AMDG Yang Zhang wrote:
Hi, I can't figure out how to properly use noncopyable. I'm always getting things like "has a non-virtual destructor" warnings.
Do you have a simple test case?
In fact, compiling noncopyable_test.cpp gives me:
<snip error message>
Any hints? Thanks in advance.
noncopyable_test.cpp isn't supposed to compile. Did you read the comment? // This program demonstrates compiler errors resulting from trying to copy // construct or copy assign a class object derived from class noncopyable. In Christ, Steven Watanabe
Steven Watanabe wrote:
AMDG
Yang Zhang wrote:
Hi, I can't figure out how to properly use noncopyable. I'm always getting things like "has a non-virtual destructor" warnings.
Do you have a simple test case?
$ cat noncopyable.cc
#include
In fact, compiling noncopyable_test.cpp gives me:
<snip error message>
Any hints? Thanks in advance.
noncopyable_test.cpp isn't supposed to compile. Did you read the comment?
// This program demonstrates compiler errors resulting from trying to copy // construct or copy assign a class object derived from class noncopyable.
Sorry, I did not. -- Yang Zhang http://www.mit.edu/~y_z/
At 2:12 AM -0500 2/22/09, Yang Zhang wrote:
Yang Zhang wrote:
Hi, I can't figure out how to properly use noncopyable. I'm always getting things like "has a non-virtual destructor" warnings.
Do you have a simple test case?
$ cat noncopyable.cc #include
using namespace boost; class C : noncopyable {}; int main() { return 0; } $ g++ -Wall -g3 -o noncopyable noncopyable.cc [...] -Weffc++ [...] cc1plus: warnings being treated as errors noncopyable.cc:3: error: base class âclass boost::noncopyable_::noncopyableâ has a non-virtual destructor
I understand that this shouldn't be a problem as noncopyable is not meant to be used directly as any variable's static type, but I do require enabling this warning.
The warning is due to the use of -Weffc++: Warn about violations of the following style guidelines from Scott Meyers' Effective C++ book: ... * Item 14: Make destructors virtual in base classes. ... This is a case where that admonition is wrong. (It is a style guideline after all, and not necessarily prescriptive.) You might be able to suppress this warning by using -Wno-nonvirtual-dtors, but that may suppress warnings that you would actually like to see. This is one of the reasons why we're not using -Weffc++ where I work. (I think I recall there being some boost libraries that run afoul of item 7 "Never overload &&, ||, or ,." too.)
On Sunday 22 February 2009 08:34:23 Kim Barrett wrote:
The warning is due to the use of -Weffc++:
Warn about violations of the following style guidelines from Scott Meyers' Effective C++ book:
... * Item 14: Make destructors virtual in base classes. ...
This is a case where that admonition is wrong. (It is a style guideline after all, and not necessarily prescriptive.) You might be able to suppress this warning by using -Wno-nonvirtual-dtors, but that may suppress warnings that you would actually like to see. This is one of the reasons why we're not using -Weffc++ where I work. (I think I recall there being some boost libraries that run afoul of item 7 "Never overload &&, ||, or ,." too.)
Isn't that admonishing simple wrong in all cases where the the base class is private (and maybe protected, I've never used that)? I mean, when exactly would you risk deleting a class through a pointer or reference to a private base class? I'd say that this is a gcc bug :) But I don't claim to be the ultimate C++ expert. -- kind regards, Esben
2009/2/23 Esben Mose Hansen
Isn't that admonishing simple wrong in all cases where the the base class is private (and maybe protected, I've never used that)? I mean, when exactly would you risk deleting a class through a pointer or reference to a private base class?
struct Base {}; void Foo(Base* base) { delete base; } struct Derived : private Base { void Bar() { Foo(this); // Oops. } }; Even with private inheritance there is a risk for an object to be deleted through the pointer to base. Although, of course, with public inheritance this risk is much higher. Roman Perepelitsa.
participants (5)
-
Esben Mose Hansen
-
Kim Barrett
-
Roman Perepelitsa
-
Steven Watanabe
-
Yang Zhang