问题
This is supposed to be a trivial question but I could not find it explicitly on stackoverflow.
The following will be defined implicitly if not provided by the user.
- default (parameterless) constructor
- copy constructor
- copy assignment operator
- destructor
But I have read somewhere (which I cant seem to find now), that there are some conditions where the compiler will not implicitly implement them.
What are these conditions?
回答1:
The Default Constuctor (e.g., X()
) will not be implicitly generated if:
- you have explicitly declared any constructor at all
- there is a data member that is not default-constructible (such as a reference, a
const
object, or a class with no or inaccessible default constructor) - (C++11) you have explicitly told the compiler to not generate one using
X() = delete;
The Copy Constructor (e.g., X(const X&)
) will not be implicitly generated if:
- you have explicitly declared a copy constructor (for class
X
a constructor takingX
,X&
orconst X&
) - there is a data member that is not copy-constructible (such as a class with no or inaccessible copy constructor)
- the base class is not copy-constructible
- (C++11) you have declared a move constructor or move assignment operator
- (C++11) you have explicitly told the compiler to not generate one using
X(const X&) = delete;
The Copy Assignment Operator (e.g., X& operator=(const X&)
) will not be implicitly generated if:
- you have explicitly declared a copy assignment operator (for class
X
anoperator=
takingX
,X&
orconst X&
) - there is a data member in your class that is not copy-assignable (such as a reference, a
const
object, or a class with no or inaccessible assignment operator) - the base class is not copy-assignable
- (C++11) you have declared a move constructor or move assignment operator
- (C++11) you have explicitly told the compiler to not generate one using
X& operator=(const X&) = delete;
The Destructor (e.g., ~X()
) will not be implicitly generated if:
- you have explicitly declared a destructor
- (C++11) you have explicitly told the compiler to not generate one using
~X() = delete;
The Move Constructor (C++11) (e.g., X(X&&)
) will not be implicitly generated if:
- you have explicitly declared a move constructor (for class
X
, a constructor takingX&&
) - you have declared a copy assignment operator, copy constructor, destructor, or move assignment operator
- there is a data member in your class that cannot be move-constructed (is
const
, is a reference, or has a deleted, inaccessible, or ambiguous move constructor) - the base class cannot be move-constructed
- you have explicitly told the compiler to not generate one using
X(X&&) = delete;
The Move Assignment Operator (C++11) (e.g., X& operator=(X&&)
) will not be implicitly generated if:
- you have explicitly declared a move assignment operator (for class
X
, anoperator=
takingX&&
) - you have declared a copy assignment operator, copy constructor, destructor, or move constructor
- there is a data member in your class that cannot be move-assigned (is
const
, is a reference, or has a deleted, inaccessible, or ambiguous move assignment operator) - the base class cannot be move-assigned
- you have explicitly told the compiler to not generate one using
X& operator=(X&&) = delete;
来源:https://stackoverflow.com/questions/15590832/conditions-under-which-compiler-will-not-define-implicits-constructor-destruct