Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2013 does not delete the copy constructor when a user-defined move constructor is provided

I appreciate the C++11 standard dictates:

If the class definition does not explicitly declare a copy constructor, 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 defined as defaulted.

(actually copied from here)

The following code:

#include <iostream>

struct C
{
    int x = 1;

    C()
    {
    }

    C(C&&)
    {
    }
};

int main()
{
    const C c;
    C c2(c);

    std::cout << c.x << " " << c2.x << std::endl;

    return 0;
}

does not compile on gcc 4.9.0, but compiles just fine on Visual Studio 2013 (Compiler Version 18.00.21005.1 for x86). Is this yet another Visual Studio violation of the standard, or am I doing something wrong this time? If this is a violation of the standard, is there a tracking bug or any source where this behaviour is documented?

like image 877
gd1 Avatar asked May 08 '15 09:05

gd1


1 Answers

You're not doing anything wrong, and your interpretation of the standard is correct. Visual C++ 2013 indeed does not implement these rules properly.

A relevant bug report is here:

Default copy constructor is generated even when a custom move constructor is defined [c++11]

It's marked as Won't Fix and the comment from the development team is:

Visual Studio 2013 indeed does not fully implement the C++11 rules governing special member functions and move operations. We will include a fix for this bug in the next major release of Visual Studio.

The good news is that things seem to be working properly in Visual C++ 2015 RC. I've just verified that your code triggers both compiler and IntelliSense errors. The compiler diagnostic is:

error C2280: 'C::C(const C &)': attempting to reference a deleted function

(From what I've tested during the past few months, MSVC14 is shaping up as a pretty good C++ compiler - lots of standard compliance issues have been fixed.)

like image 119
bogdan Avatar answered Nov 06 '22 10:11

bogdan