Home  >  Article  >  Backend Development  >  Why Does C Compiler Error C2280 \"attempting to reference a deleted function\" Occur in Visual Studio 2015 but Not 2013?

Why Does C Compiler Error C2280 \"attempting to reference a deleted function\" Occur in Visual Studio 2015 but Not 2013?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-27 12:14:02743browse

Why Does C   Compiler Error C2280

C Compiler Error C2280 "attempting to reference a deleted function" in Visual Studio 2013 and 2015

In Visual Studio 2013, the following code snippet compiles without errors:

class A
{
public:
   A(){}
   A(A &&){}
};

int main(int, char*)
{
   A a;
   new A(a);
   return 0;
}

However, the same code snippet generates an error in Visual Studio 2015:

1>------ Build started: Project: foo, Configuration: Debug Win32 ------
1>  foo.cpp
1>c:\dev\foo\foo.cpp(11): error C2280: 'A::A(const A &)': attempting to reference a deleted function
1>  c:\dev\foo\foo.cpp(6): note: compiler has generated 'A::A' here
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This is because the C standard specifies that if a class declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted.

To fix this problem, you can explicitly provide a copy constructor and copy assignment operator:

class A
{
public:
   A(){}
   A(A &&){}
   A(const A&) = default;
   A& operator=(const A&) = default;
};

This will allow you to copy-construct and copy-assign objects of class A.

The above is the detailed content of Why Does C Compiler Error C2280 \"attempting to reference a deleted function\" Occur in Visual Studio 2015 but Not 2013?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn