Compiler Error C2280 "Attempting to Reference a Deleted Function" in Visual Studio 2015
The Visual Studio 2015 compiler, unlike its 2013 predecessor, automatically generates a deleted copy constructor for classes defining a move constructor or move assignment operator. This behavior is mandated by the C standard to prevent accidental copying in situations where moving is preferred.
In your code snippet, the class A has a move constructor A(A &&), which in turn implies a deleted copy constructor as per the standard. Consequently, the new A(a) expression in main triggers error C2280.
To resolve this issue, you can explicitly declare the copy constructor in A:
<code class="cpp">class A { public: A() {} A(A &&) {} A(const A&) = default; // Explicitly declare the copy constructor as defaulted };</code>
Alternatively, if you genuinely intend to disable copying and only allow moving, you can declare the copy constructor and copy assignment operator as deleted:
<code class="cpp">class A { public: A() {} A(A &&) {} A(const A&) = delete; A& operator=(const A&) = delete; // Delete copy assignment operator };</code>
Remember, if you choose to disable copying, you will also need to declare a move assignment operator and destructor to comply with the Rule of Five.
The above is the detailed content of Why Am I Getting Compiler Error C2280 \'Attempting to Reference a Deleted Function\' in Visual Studio 2015?. For more information, please follow other related articles on the PHP Chinese website!