How to delete all implicit (default) methods of class in C++? - c++

C++ compiler in certain conditions for class adds implicit default constructor, copy constructor, destructor, conversion operators, assignment operators, etc. And other implicit methods.
How do I disable/delete all possible implicit methods in shortest way possible?
I don't want my class to be un-copyable, I want just these default methods to be disabled so that compiler throws compile error and I implement them by myself. I just want to have over-control, so that compiler doesn't do any silent work about my class.
For example I can do next thing (this is just a dummy example, in real life I can have any complex class):
class C {
private:
C() = delete;
C(C const & other) = delete;
C & operator = (C const & other) = delete;
};
But this is long way, also I can forget deleting some of methods signatures and I need to delete all of them. So that I reimplement all methods from scratch by myself.
And I need to see errors for all methods that other code needs/uses and that I have not yet implemented, instead of compiler silently implementing those methods for me.
Also if I delete default methods like I did above in my example, how can I be sure that there are no other silently created methods? Is there online a list of 100% all standard methods created silently by compiler?

You may go for a mixin approach. Define the cumbersome class once
class DeleteAllSpecialMemebers {
private:
DeleteAllSpecialMemebers() = delete;
DeleteAllSpecialMemebers(DeleteAllSpecialMemebers const &) = delete;
DeleteAllSpecialMemebers & operator = (DeleteAllSpecialMemebers const &) = delete;
DeleteAllSpecialMemebers(DeleteAllSpecialMemebers &&) = delete;
DeleteAllSpecialMemebers & operator = (DeleteAllSpecialMemebers &&) = delete;
protected:
struct token { explicit token() = default; };
DeleteAllSpecialMemebers(token) {}
};
Then inherit from it:
class C : private DeleteAllSpecialMemebers{
public:
C(int) : DeleteAllSpecialMemebers(token{}) {}
};
The extra token type is to allow deriving classes to implement other constructors, such as C::C(int) (or even a user-provided C::C()).
See it live.

The shortest option might be to have a reusable dummy base class:
struct NoSpecialMember {
NoSpecialMember(const NoSpecialMember&) = delete;
NoSpecialMember& operator=(const NoSpecialMember&) = delete;
NoSpecialMember(NoSpecialMember&&) = delete;
NoSpecialMember& operator=(NoSpecialMember&&) = delete;
~NoSpecialMember() = default;
};
It's sufficient to inherit privately;
class Test : private NoSpecialMember {};
with the following result:
static_assert(!std::is_copy_constructible_v<Test>);
static_assert(!std::is_copy_assignable_v<Test>);
static_assert(!std::is_move_constructible_v<Test>);
static_assert(!std::is_move_assignable_v<Test>);

Define helper and use it as field of class where it should be disabled:
struct DisableCopyAssign
{
DisableCopyAssign(char){}
DisableCopyAssign(const DisableCopyAssign&) = delete;
DisableCopyAssign(DisableCopyAssign&&) = delete;
};
class Foo
{
private:
DisableCopyAssign dummy;
};
https://gcc.godbolt.org/z/ce8sjq

Related

What is the minimal class to extend for a no copy / no move type?

Let's say I want to create some classes to manage resources that shouldn't be copied nor moved, which would be the minimal class to extend and avoid mistakes?
The idea is that by extending the class, I end on the safe side of the 0/3/5 rules.
I have this in mind, which apparently works.
class NOCOPYNOMOVE {
NOCOPYNOMOVE(NOCOPYNOMOVE &v) = delete;
NOCOPYNOMOVE(NOCOPYNOMOVE &&v) = delete;
NOCOPYNOMOVE& operator=(NOCOPYNOMOVE &r) = delete;
NOCOPYNOMOVE& operator=(NOCOPYNOMOVE &&r) = delete;
};
class Foo: private NOCOPYNOMOVE {
public:
Foo() {}
~Foo() {}
};
Don't need to delete move constructor, see https://stackoverflow.com/a/38820178/2945027
Avoid ALL_CAPS name, as it is conventionally by every convention used for macros
There's no reason to omit const in copy constructor/assignment, so the usual form should be preferred
If classes would inherit from some NOCOPYNOMOVE in some namespace, it may trigger unintended ADL. boost::noncopyable solves it by putting the definition in noncopyable_ namespace, see the implementation
I'd prefer just not having some base, instead spelling out these two or three lines in the target class:
class Foo {
public:
Foo() {}
~Foo() {}
Foo(const Foo&) = delete;
Foo& operator=(const Foo&) = delete;
};

Noncopyable and Nonmovable together?

I want to create a base class both noncopyable and nonmovable.
class noncopyable {
protected:
noncopyable() = default;
~noncopyable() = default;
noncopyable(noncopyable const &) = delete;
noncopyable &operator=(noncopyable const &) = delete;
};
class nonmovable {
protected:
nonmovable() = default;
~nonmovable() = default;
nonmovable(nonmovable &&) = delete;
nonmovable &operator=(nonmovable &&) = delete;
};
Is there any scenario that a class should be both noncopyable and nonmovable?
class non : public noncopyable, public nonmovable {
};
class foo : public non {
};
If there, what should be the convenient name for the "non" class here?
It has been argued, for instance here, that nonmoveable as demonstrated is a bad idea in the first place.
There are four reasonable options:
Type is naturally copyable and moveable.
Type is non-copyable, but moveable.
This happens often if it is managing a resource, and copying the resource is impossible, or so expensive as to be undesirable.
Type has no special moveability -- move ctor simply calls copy ctor.
This mostly happens if for technical reasons, you don't want to create an "empty state" for objects of this type, or there is no cheap / natural way to leave the moved-from object in any acceptable state.
Type is neither copyable nor moveable.
This may happen with a static singleton or something like this, where moving or copying it is something that should never happen in your program and you want to prevent from happening with a compile-time error.
If the type is copyable, but moving it fails, this is strictly a bad arrangement, and instead move should be made to fallback to copy. There's no advantage to making a move fail when a copy would be okay, it only inhibits generic programming.
So maybe you should only have "noncopyable" and "non", but not "nonmoveable" ?
While a "noncopyable" will work, an "nonmovable" base class will not provide what you expect:
#include <utility>
#include <iostream>
struct nonmovable
{
nonmovable() = default;
nonmovable(const nonmovable&) { std::cout << "copy\n"; }
nonmovable& operator = (const nonmovable&) { std::cout << "asign\n"; return *this; }
nonmovable(nonmovable&&) = delete;
nonmovable& operator = (nonmovable&&) = delete;
};
struct X : nonmovable {};
int main()
{
nonmovable n0;
nonmovable n1(n0);
// error: use of deleted function ‘nonmovable::nonmovable(nonmovable&&)’:
//nonmovable n2(std::move(n0));
X x0;
X x1(x0);
// However, X has a copy constructor not applying a move.
X x2(std::move(x0));
}
In addition, move construction and move assignment must be enabled explicitly after deletion of the copy constructor, if desiered:
struct noncopyable
{
noncopyable() = default;
// Deletion of copy constructor and copy assignment makes the class
// non-movable, too.
noncopyable(const noncopyable&) = delete;
noncopyable& operator = (const noncopyable&) = delete;
// Move construction and move assignment must be enabled explicitly, if desiered.
noncopyable(noncopyable&&) = default;
noncopyable& operator = (noncopyable&&) = default;
};
The, names "noncopyable" and "nonmovable" itself are good descriptive names. However, "boost::noncopyable" is both (non copyable and non movable), which might be a better (historical) design decision.
as an example - signleton pattern. Also, if u defined copy-constructor/assignment operator/destructor, move-constructor/assignment wont be generated.

Most concise way to disable copying class in C++11

I have a problem dealing with deprecated since C++11 default generation of copy constructor and copy assignment operator when there is a user-defined destructor.
For most sufficiently simple classes default-generated constructors, operators and destructor are fine. Consider the following reasons to declare destructor:
Making trivial destructor virtual in base class:
// header
class Base1 { public: virtual ~Base1() = default; };
class Base2 { public: virtual ~Base2(); };
// source
Base2::~Base2() = default;
Would all 4 copy and move special methods be generated by compiler in these cases? If yes, then I think it is fine and there is no need to complicate Base1 or Base2.
Printing debug message in destructor:
// header
class D { public: ~D(); };
// source
D::~D() {
#ifdef DEBUG_THIS
std::cout << "D was destructed." << std::endl;
#endif
}
I believe that in this case copy constructor and assignment operator would be generated; but move constructor and assignment operator would not. I want to avoid using deprecated default-generating and disable copying of D. I also want to avoid flooding D with 4 deleted declarations. Is disabling only one copy constructor enough? Is it a good style?
With C++11, a clean way is to follow the pattern used in boost (see here)
You basically create a base class where copy constructor and copy assignment are deleted, and inherit it:
class non_copyable
{
protected:
non_copyable() = default;
~non_copyable() = default;
non_copyable(non_copyable const &) = delete;
void operator=(non_copyable const &x) = delete;
};
class MyClass: public non_copyable
{
...
}
Deleting the copy-constructor and copy-assignment operator is the simplest and clearest way to disable copying:
class X
{
X(X const &) = delete;
void operator=(X const &x) = delete;
};
I don't follow what you are talking about with virtual destructors in the question body . It sounds like you're asking for a way to make your code take up fewer characters of source code, but also be more cryptic to anybody looking at it.
If the list of deleted functions bothers you, you could hide them behind a macro, I guess.
#define NON_COPYABLE_NOR_MOVABLE(T) \
T(T const &) = delete; \
void operator=(T const &t) = delete; \
T(T &&) = delete;
Only copy constructor and copy assignment operator will be generated when destructor is explicitly defaulted. And even then their generation is deprecated. So, in order to have virtual destructor and all default methods, one should write the following:
struct Base
{
Base()=default;
virtual ~Base() = default;
Base(const Base&)=default;
Base& operator=(const Base&)=default;
Base(Base&&)=default;
Base& operator=(Base&&)=default;
};
I would definitely use a macro for more than one such Base class.
In case when destructor is defined by user, 2 special methods are still generated. There are the following ways to disable deprecated generating copy constructor and copy assignment operator:
delete move constructor OR move assignment operator (not quite self-explanatory but very short):
Base(Base&&)=delete; // shorter than deleting assignment operator
delete both copy constructor and copy assignment operator:
Base(const Base&)=delete;
Base& operator=(const Base&)=delete;
Note that you have to explicitly declare default constructor if you need it, e.g. Base()=default;.
Macro or inheriting special class can be used as well for this purpose but I personally prefer deleting move constructor to implementing my own macro or base class. When using Qt or boost, I would prefer Q_DISABLE_COPY(Base) and inheriting boost::noncopyable respectively, because they are already implemented, widely known and recognizable.
http://accu.org/index.php/journals/1896 - detailed explanation and rationale for these issues.
You can do it by this(which is used by Caffe: a fast open framework for deep learning):
// Disable the copy and assignment operator for a class.
#define DISABLE_COPY_AND_ASSIGN(classname) \
private:\
classname(const classname&);\
classname& operator=(const classname&)
Usage example:
class CNoCopyable{
public:
CNoCopyable(int i):m_d(i){}
private:
int m_d;
// add this line(pass class name)
DISABLE_COPY_AND_ASSIGN(CNoCopyable);
};

Is it possible to declare operator= private and have it synthesized by the compiler at the same time in C++

I am happy with the operator =, which is synthesized by the compiler automatically. But I want it to be private and do not want to bloat my code with page long definitions of the type
Foo& Foo::operator= (const Foo& foo)
{
if (this == &foo)
return *this;
member1_ = foo.member1_;
member2_ = foo.member2_;
member3_ = foo.member2_;
...
member1000_ = foo.member1000_;
return *this;
}
Please, is there a way to do this?
In C++11 it is:
class Foo
{
Foo& operator=(const Foo& source) = default;
public:
// ...
};
Unfortunately, most compilers haven't implemented this part of the new standard yet.
Another option is to use the Pimpl idiom.
class Foo {
public:
Foo() : pImpl(new FooImpl) {}
// ... Foo's public interface, same as before
private:
Foo& operator=(const Foo& source); //- Foo's assignment operator is private
struct FooImpl;
boost::scoped_ptr<FooImpl> pImpl;
};
struct FooImpl {
// ... all the private data members that use to be in Foo
// Note: using the compiler generated copy assignment operator
};
The copy assignment operator is private from the POV of Foo clients but you can still leverage the compiler generated copy assignment via FooImpl. The tradeoff comes when implementing Foo's member functions as you now have to access the data through the pImpl pointer.

Can I prevent object assignment?

I want to ensure that the following type of call is illegal:
MyClass me;
MyClass you;
me = you; // how to make this illegal?
Is it possible?
Declare the assignment operator private:
class A {
private:
void operator=(const A&);
...
};
but don't provide an implementation - you will get a compile or link-time error if you try to perform an assignment to A.
I prefer to use a macro to do this. This also prevents copying, by making the copy constructor private too:
#define CANNOT_COPY( class ) \
private: \
class(const class&); \
void operator=(const class &) \
I can then say things like:
class A {
CANNOT_COPY( A );
...
};
which is easy to read and easy to search for.
declare the assignment operator as private.
Yes - define a private assignment operator (operator=) or derive from the handy boost::noncopyable class.
As of C++11, my understanding is that the preferred solution is to use the '= delete' construct:
class MyClass {
MyClass (const MyClass&) = delete; // Disable copy constructor
MyClass& operator=(const MyClass&) = delete; // Disable assignment
...
}
Then
MyClass me;
MyClass you;
me = you; // won't compile
Use const.
MyClass const me;
MyClass you;
me = you; // compilation error
I use to derive my noncopyable classes from a common noncopyable class. If not using boost, I usually use this shortcut:
class NonCopyable
{
protected:
/**
* Protected default constructor
*/
NonCopyable() {}
/**
* Protected destructor
*/
~NonCopyable() {}
private:
/**
* Private copy constructor
*/
NonCopyable( const NonCopyable& );
/**
* Private assignment operator
*/
const NonCopyable& operator=( const NonCopyable& );
};
Note that neither the copy constructor nor the assignment operators have an implementation.
The other answers here are all correct. However, it is important to note that you are only prohibiting the specific code you mentioned. Someone can still come along and make a duplicate of your class by using memcpy or other strategies.