user4520
user4520

Reputation: 3457

Why does MSVC 12.0 ignore private constructors if the copy assignment operator is deleted?

Consider the following code:

class Test
{
private:
    Test() = default;
};

Test t;

int main() { }

Both MSVC 12 and 14 (Visual Studio 2013 and 2015, respectively) refuse to compile this, as expected - the constructor is private and so we cannot create objects of that type outside of the class itself.

However, let's now make one small change:

class Test
{
private:
    Test() = default;
    Test operator=(const Test& rhs) = delete;
};

Test t;

int main() { }

To my surprise, it will compile fine in MSVC 12 (14 still gives the same error). Why is it so? My first thought was that maybe this was standard behavior in an older version of C++, but then I realized that the very concept of deleting constructors was only introduced in C++11.

Is this a bug?

Upvotes: 2

Views: 130

Answers (1)

Daren Coffey
Daren Coffey

Reputation: 101

Well, MSVC 12 does have partial c++ 11 support, but I do believe that compiling is a bug. I encountered something similar using MSVC 12 where I could access private fields from non-static template functions, when I clearly shouldn't have. It was a coding oversight that strangely compiled. I thought it was weird so I compiled it with GCC and sure enough GCC said "No Good!". I think it was fixed in the November CTP though, since I can't seem replicate it.

Upvotes: 1

Related Questions