Is there any way to create a generic macro to delete copy ctor, assignment operator
//Below two lines are class specific, and looking to replace with some generic way
//without mentioning hardcoded class name
#define NO_COPY Original(_In_ const Original&) = delete;
#define NO_ASSIGNMENT Original& operator=(_In_ const Original&) = delete;
//simple class
class Original
{
public:
NO_COPY;
NO_ASSIGNMENT;
};
boost::noncopyable
is this :
class noncopyable { protected: #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && !defined(BOOST_NO_CXX11_NON_PUBLIC_DEFAULTED_FUNCTIONS) BOOST_CONSTEXPR noncopyable() = default; ~noncopyable() = default; #else noncopyable() {} ~noncopyable() {} #endif #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) noncopyable( const noncopyable& ) = delete; noncopyable& operator=( const noncopyable& ) = delete; #else private: // emphasize the following members are private noncopyable( const noncopyable& ); noncopyable& operator=( const noncopyable& ); #endif };
Usage is
struct X : private boost::noncopyable {};
If you do not want boost, it is straightforward to write your own my::noncopyable
. The protected
constructor and destructor are merely to make clear that the class is meant to be used as base class. Without the pre-C++11 conditions, the class is just:
class noncopyable
{
protected:
constexpr noncopyable() = default;
~noncopyable() = default;
noncopyable( const noncopyable& ) = delete;
noncopyable& operator=( const noncopyable& ) = delete;
};
The macro you are asking for does not exist. As Sergey pointed out in a comment, you would at least have to pass the name of the class to the macro, which makes the point of typing less moot.
Last but not least, I suggest you to read this answer which makes some good points against a noncopyable
base class and for spelling out the deleted member functions. Don't try to be too lazy. Less typing is not always the way to cleaner code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.