I have a non-dangerous class. Copying this would be problematic. I want to guarantee that it will never be copied, so I made its copy constructor deleted :
class A { public: A(); A(const A&) = delete; }; A fun() { return A(); }; int main() { A a = fun(); };
Unfortunately, g ++ does not compile this for a reason:
t.cc: In function 'A fun()': t.cc:8:12: error: use of deleted function 'A::A(const A&)' return A(); ^ t.cc:4:5: note: declared here A(const A&) = delete; ^ t.cc: In function 'int main()': t.cc:12:13: error: use of deleted function 'A::A(const A&)' A a = fun(); ^ t.cc:4:5: note: declared here A(const A&) = delete; ^
But this is a very clear situation when you need to use copy, so the copy constructor should never be called. Why is this so?
c ++ c ++ 11 copy-elision
peterh
source share