Compiler warning (level 4) C5267
definition of implicit copy constructor/assignment operator for 'type' is deprecated because it has a user-provided assignment operator/copy constructor
Remarks
The C++ Standard deprecated (but didn't remove) the implicit generation of copy and assignment operators under some conditions. The MSVC compiler still generates the copy and assignment operators under those conditions, but may change its behavior in the future if the standard removes the deprecated behavior. The purpose of this warning is to help future proof your code if the committee decides to remove this functionality.
The relevant sections in the C++ standard are:
- class.copy.ctor paragraph 6, which says: "If the class definition does not explicitly declare a copy constructor, a nonexplicit one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defaulted. The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor."
- Annex D D.8, which says: "The implicit definition of a copy constructor as defaulted is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor. The implicit definition of a copy assignment operator as defaulted is deprecated if the class has a user-declared copy constructor or a user-declared destructor. It's possible that future versions of C++ will specify that these implicit definitions are deleted."
Example
The following code shows warning C5267 when an implicitly generated special function is called but isn't explicitly defined. Both /W4
and /w45267
are required to produce this warning.
// C5267.cpp
// compile using: /W4 /w45267
struct CopyCtorOnly
{
CopyCtorOnly() = default;
CopyCtorOnly(const CopyCtorOnly&) {} // C5267
};
struct CopyAssignOpOnly
{
CopyAssignOpOnly() = default;
CopyAssignOpOnly& operator=(const CopyAssignOpOnly&) // C5267
{
return *this;
}
};
int main()
{
CopyCtorOnly a1, a2;
a1 = a2; // Calls deprecated copy assignment operator
CopyAssignOpOnly b1;
CopyAssignOpOnly b2 = b1; // Calls deprecated copy constructor
}
To resolve this issue, explicitly define the missing copy constructor or copy assignment operator.