Skip to content

Document warning C5267 #4807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/cpp/explicitly-defaulted-and-deleted-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This is convenient for simple types, but complex types often define one or more
> - If a copy constructor or destructor is explicitly declared, then automatic generation of the copy-assignment operator is deprecated.
> - If a copy-assignment operator or destructor is explicitly declared, then automatic generation of the copy constructor is deprecated.
>
> In both cases, Visual Studio continues to automatically generate the necessary functions implicitly, and does not emit a warning.
> In both cases, Visual Studio continues to automatically generate the necessary functions implicitly, and does not emit a warning by default. Since Visual Studio 2022 version 17.7, [C5267](../error-messages/compiler-warnings/c5267.md) can be enabled to emit a warning.

The consequences of these rules can also leak into object hierarchies. For example, if for any reason a base class fails to have a default constructor that's callable from a deriving class—that is, a **`public`** or **`protected`** constructor that takes no parameters—then a class that derives from it cannot automatically generate its own default constructor.

Expand Down
56 changes: 56 additions & 0 deletions docs/error-messages/compiler-warnings/c5267.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: "Compiler warning C5267"
description: Compiler warning C5267 description and solution.
ms.date: 11/08/2023
f1_keywords: ["C5267"]
helpviewer_keywords: ["C5267"]
---
# Compiler warning 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 did not 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](https://eel.is/c++draft/class.copy.ctor#6) which says: "If the class definition does not explicitly declare a copy constructor, a non-explicit 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](https://eel.is/c++draft/depr.impldec#1) 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 is 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:

```cpp
// 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.

## See also

[Explicitly Defaulted and Deleted Functions](../../cpp/explicitly-defaulted-and-deleted-functions.md)
Loading