|
Boost : |
Subject: [boost] [boost::noncopyable] compiler warnings with the -Weffc++ flags
From: Anand Lakshminath (anandrl_at_[hidden])
Date: 2009-06-11 16:42:15
Hi,
Deriving from the boost::noncopyable class generates warnings when I
compile with the -Weffc++ (effective C++) flags.
There are two types of warnings:
1) Non-virtual destructor warning (base class 'class
boost::noncopyable_::noncopyable' has a non-virtual destructor).
Here's a testcase:
#include <boost/noncopyable.hpp>
#include <iostream>
namespace
{
class DontTreadOnMe : private boost::noncopyable
{
public:
DontTreadOnMe() { std::cout << "defanged!" << std::endl; }
}; // DontTreadOnMe
} // unnamed namespace
main()
{
DontTreadOnMe object1;
}
g++ -I/vobs/3rdparty/BOOST/boost_1_38_0/boost_inc -Weffc++ main.cpp
main.cpp:10: warning: base class 'class
boost::noncopyable_::noncopyable' has a non-virtual destructor
2) I have disabled the copy constructor/assignment operator in a class
that has pointer data members by deriving from boost::noncopyable.
However, the compiler generates the following warning:
warning: 'class<unnamed>::DontTreadOnMe' has pointer data members
main.cpp:10: warning: but does not override
'<unnamed>::DontTreadOnMe(const <unnamed>::DontTreadOnMe&)'
This kind of forces me to explicitly override the copy constructor and
assignment operator instead of deriving from noncopyable. Here's the
full test case:
#include <boost/noncopyable.hpp>
#include <iostream>
namespace
{
class DontTreadOnMe : private boost::noncopyable
{
public:
DontTreadOnMe() { std::cout << "defanged!" << std::endl; }
private:
char* buf;
}; // DontTreadOnMe
} // unnamed namespace
main()
{
DontTreadOnMe object1;
}
g++ -I/vobs/3rdparty/BOOST/boost_1_38_0/boost_inc -Weffc++ main.cpp
main.cpp:10: warning: base class 'class
boost::noncopyable_::noncopyable' has a non-virtual destructor
main.cpp:10: warning: 'class<unnamed>::DontTreadOnMe' has pointer data members
main.cpp:10: warning: but does not override
'<unnamed>::DontTreadOnMe(const <unnamed>::DontTreadOnMe&)'
main.cpp:10: warning: or 'operator=(const <unnamed>::DontTreadOnMe&)'
Is there a workaround and/or fix? I am using gcc 4.0.1.
Thanks,
Anand
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk