Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"deleting" copy ctor/assignment in C++11

In VS 2010 SP1, the following:

class Foo
{
public:
    Foo() { }
    Foo(Foo const&) = delete; // Line 365
    Foo& operator=(Foo const&) = delete; // Line 366
};

does not compile. It complains:

CPPConsole.cpp(365): error C2059: syntax error : ';'
CPPConsole.cpp(365): error C2238: unexpected token(s) preceding ';'
CPPConsole.cpp(366): error C2059: syntax error : ';'
CPPConsole.cpp(366): error C2238: unexpected token(s) preceding ';'

Is this not supported yet? The strange thing is, Intellisense seems to recognize this construct. It says "IntelliSense: function "Foo::operator=(const Foo &)" (declared at line 366) cannot be referenced -- it is a deleted function"

What am I missing?

like image 922
ForeverLearning Avatar asked Aug 17 '11 17:08

ForeverLearning


People also ask

When should you delete copy constructor?

When to delete copy constructor and assignment operator? Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant. It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour.

What is deleted copy constructor?

The copy constructor and copy-assignment operator are public but deleted. It is a compile-time error to define or call a deleted function.

What is a deleted function C++?

A deleted function is implicitly inline. A deleted definition of a function must be the first declaration of the function. For example: class C { public: C(); }; C::C() = delete; // Error, the deleted definition of function C must be // the first declaration of the function.

What is default and delete in C++?

Using = delete is for prohibiting the use of one of the functions, and that = default is used if you want to be explicit to the compiler on where to use the defaults for these functions.


1 Answers

VS 2010 has something of a dual personality. Specifically, it actually has two entirely separate compiler front ends.

When you compile the code, that's done with Microsoft's own compiler, which goes all the way back to MS C 3.0 for MS-DOS, released ~3 decades ago (in case you're wondering why it was 3.0, MS sold a re-labeled version of Lattice C before that).

Up until VS 2008, the parsing in the IDE was rather primitive compared to the compiler's, so it didn't parse a lot of more sophisticated C++ quite correctly. They decided that was unacceptable, and rather than try to upgrade the IDE's existing parser to match the compiler's, they licensed the EDG compiler front-end.

This gives more or less the opposite situation: the IDE's parser for Intellisense is now considerably closer to conforming than the one on the compiler, and recognizes a fair number of C++0x constructs that the compiler doesn't.

There's a little more to the story than just that though: the EDG compiler front-end supports a switch to make it act more like VC++, including emulating a fair number of VC++ bugs. Although I don't have data to confirm it, my assumption would be that Microsoft uses that capability. Since that's based on EDG taking the VC++ compiler, and emulating its bugs, it's probably a fair guess that (at least usually) EDG's VC++ emulation will run about a version behind VC++ itself. That gives the somewhat paradoxical situation where EDG (in normal use) is normally quite a bit ahead of VC++, but the version MS uses in the IDE is probably doing to be at least slightly behind most of the time.

like image 189
Jerry Coffin Avatar answered Oct 04 '22 11:10

Jerry Coffin