In a probject I used code similar to the following:
class C {
public:
C() {}
C(const C&) = delete;
};
C f() {
return C();
}
int main() {
f();
}
In every previous Visual C++ compiler I used (up to 2013), that has never been a problem. But when I try to compile it with the new Visual C++ 2015 compiler I get the following error:
1>c:\devel\c++11\playground\main.cpp(10): error C2280: 'C::C(const C &)': attempting to reference a deleted function
1> c:\devel\c++11\playground\main.cpp(6): note: see declaration of 'C::C'
I'm not sure why it previously worked but I assume that because of return value optimization the default constructor was called and not the copy constructor.
Is the code I used even legal C++? And if not, what would be the correct way of implementing this code without requiring a copy constructor for my class C
? I could of course use a move constructor but then I assume the code would have never been valid C++ before C++11?