Skip to content

Commit e43632b

Browse files
author
Colin Robertson
committed
Massive update to C2280
1 parent 4a887c5 commit e43632b

1 file changed

Lines changed: 100 additions & 10 deletions

File tree

‎docs/error-messages/compiler-errors-1/compiler-error-c2280.md‎

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,111 @@ translation.priority.mt:
3939

4040
'*declaration*': attempting to reference a deleted function
4141

42-
The compiler detected an attempt to reference a deleted function. This error can be caused by a call to a member function that has been explicitly marked as deleted in the source code. This error can also be caused by a call to an implicit special member function that is declared and marked as deleted by the compiler.
42+
The compiler detected an attempt to reference a deleted function. This error can be caused by a call to a member function that has been explicitly marked as `= deleted` in the source code. This error can also be caused by a call to an implicit special member function of a struct or class that is automatically declared and marked as deleted by the compiler. For more information about when the compiler automatically generates default or deleted special member functions, see [Special member functions](../../cpp/special-member-functions.md).
4343

44-
When you see error C2280 in connection with a `unique_ptr`, it is almost certainly because you are attempting to invoke its copy constructor, which is a deleted function. By design, a `unique_ptr` cannot be copied.
44+
## Example: Explicitly deleted functions
45+
46+
A call to an explicitly deleted function causes this error. An explicitly deleted member function implies that the class or struct is intentionally designed to prevent its use, so to fix this issue, you should change your code to avoid it.
4547

46-
Previous versions of the compiler allowed a derived class to call member functions of indirectly-derived `private virtual` base classes. This behavior was incorrect and does not conform to the C++ standard. The compiler now issues compiler error C2280 when such a call is made.
48+
```cpp
49+
// C2280_explicit.cpp
50+
// compile with: cl /c /W4 C2280_explicit.cpp
51+
struct A {
52+
A();
53+
A(int) = delete;
54+
};
55+
56+
struct B {
57+
A a1;
58+
A a2 = A(3); // C2280, calls deleted A::A(int)
59+
// To fix, remove the call to A(int)
60+
};
61+
62+
void foo() {
63+
B b; // calls implicit B::B(void)
64+
}
65+
```
66+
67+
## Example: Uninitialized data members
68+
69+
An uninitialized `const` data member or reference type data member causes the compiler to implicitly declare a deleted default constructor. To fix this issue, initialize the data member when it is declared.
70+
71+
```cpp
72+
// C2280_uninit.cpp
73+
// compile with: cl /c C2280_uninit.cpp
74+
struct A {
75+
const int i; // uninitialized const-qualified data
76+
// causes implicit default constructor to be deleted.
77+
// To fix, initialize the value in the declaration:
78+
// const int i = 42;
79+
} a; // C2280
80+
```
81+
82+
## Example: Reference data members
83+
84+
A reference type data member causes the compiler to declare a deleted default constructor, copy constructor, and assignment operator. To fix this issue, explicitly declare the required functions.
85+
86+
```cpp
87+
// C2280_ref.cpp
88+
// compile with: cl /c C2280_ref.cpp
89+
int k = 42;
90+
struct A {
91+
A();
92+
int& ri = k; // reference data member
93+
// causes implicit assignment operator to be deleted.
94+
// To fix, declare it explicitly;
95+
// A& operator=(const A& ra) { this->ri = ra.ri; return *this; };
96+
};
97+
98+
struct B {
99+
A a;
100+
};
101+
102+
void f() {
103+
B b1, b2;
104+
b2 = b1; // C2280
105+
}
106+
```
107+
108+
## Example: Variant and volatile members
47109
48-
Previous versions of the compiler also generated default constructors and destructors for anonymous unions. These are now implicitly declared as deleted.
110+
Versions of the compiler before Visual Studio 2015 Update 2 generated default constructors and destructors for anonymous unions. These are now implicitly declared as deleted. Previous versions of the compiler also allowed non-conforming implicit definition of default copy and move constructors and default copy and move assignment operators in classes that have `volatile` member variables. The compiler now considers classes and structs that have volatile member variables to have non-trivial constructors and assignment operators, which prevents automatic generation of default implementations. When such a class is a member of a union, or an anonymous union inside of a class, the copy and move constructors and copy and move assignment operators of the union, or the class containing the anonymous union, are implicitly defined as deleted. To fix error C2280 when you attempt to construct or copy such a union or class, you must explicitly declare these operators.
49111
50-
Previous versions of the compiler allowed implicit definition of default copy and move constructors and default copy and move assignment operators in classes that have `volatile` member variables. This behavior does not conform to the C++ standard. The compiler now considers a class that has volatile member variables to have non-trivial construction and assignment operators, which prevents automatic generation of default implementations of these operators. When such a class is a member of a union, or an anonymous union inside of a class, the copy and move constructors and copy and move assignment operators of the union, or the class containing the unonymous union, are implicitly defined as deleted. An attempt to construct or copy such a union or class without explicitly defining these operators causes compiler error C2280.
112+
```cpp
113+
// C2280_variant.cpp
114+
// compile with: cl /c C2280_variant.cpp
115+
struct A {
116+
A() = default;
117+
A(A&);
118+
};
119+
120+
struct B {
121+
union {
122+
A a;
123+
int i;
124+
};
125+
// To fix this issue, define any needed
126+
// default, copy, and move constructors
127+
// and copy and move assignment operators:
128+
// B();
129+
// B(const B& b);
130+
};
131+
132+
int main() {
133+
B b1;
134+
B b2(b1); // C2280
135+
}
136+
```
51137

52-
## Example
138+
## Example: Movable deletes implicit copy
53139

54-
The compiler may create deleted special member functions implicitly in a class. For example, if a class declares a move constructor or move assignment operator, but does not explicitly declare a copy constructor, the compiler implicitly declares a copy constructor and defines it as deleted. Similarly, if a class declares a move constructor or move assignment operator, but does not explicitly declare a copy assignment operator, the compiler implicitly declares a copy assignment operator and defines it as deleted.
140+
If a class declares a move constructor or move assignment operator, but does not explicitly declare a copy constructor, the compiler implicitly declares a copy constructor and defines it as deleted. Similarly, if a class declares a move constructor or move assignment operator, but does not explicitly declare a copy assignment operator, the compiler implicitly declares a copy assignment operator and defines it as deleted. To fix this issue, you must explicitly declare these members.
55141

142+
When you see error C2280 in connection with a `unique_ptr`, it is almost certainly because you are attempting to invoke its copy constructor, which is a deleted function. By design, a `unique_ptr` cannot be copied. Use a move constructor to transfer ownership instead.
143+
56144
```cpp
57-
// C2280d.cpp
58-
// compile with: cl /c C2280d.cpp
145+
// C2280_move.cpp
146+
// compile with: cl /c C2280_move.cpp
59147
class base
60148
{
61149
public:
@@ -76,7 +164,9 @@ void copy(base *p)
76164
}
77165
```
78166
79-
## Example
167+
## Example: Indirect base members deleted
168+
169+
Previous versions of the compiler allowed a derived class to call member functions of indirectly-derived `private virtual` base classes. This behavior was incorrect and does not conform to the C++ standard. The compiler now issues compiler error C2280 when such a call is made.
80170
81171
In this example, class `top` indirectly derives from private virtual `base`. This makes the members of `base` inaccessible to `top`; an object of type `top` can't be instantiated or destroyed.
82172

0 commit comments

Comments
 (0)