C ++ Why adding a destructor to my class makes my class unmarked?

The compiler reminds me that I am using a remote function. https://ideone.com/3YAIlA

#include <memory> using namespace std; class foo { public: unique_ptr<int> p; ~foo() { } }; int main() { foo a, b; a = move(b); return 0; } 

compilation information

 prog.cpp: In function 'int main()': prog.cpp:15:4: error: use of deleted function 'foo& foo::operator=(const foo&)' a = move(b); prog.cpp:3:7: note: 'foo& foo::operator=(const foo&)' is implicitly deleted because the default definition would be ill-formed: class foo prog.cpp:3:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]' In file included from /usr/include/c++/5/memory:81:0, from prog.cpp:1: /usr/include/c++/5/bits/unique_ptr.h:357:19: note: declared here unique_ptr& operator=(const unique_ptr&) = delete; 

If I remove the destructor, my code compiles fine. https://ideone.com/UFB18P

+5
source share
1 answer

Because what the standard says . When you start adding special member functions, you prohibit the automatic generation of some others. This is in the same category of rules as for writing a constructor other than the standard one, meaning that by default it will not be automatically generated for you.

Add this:

 foo& operator=(foo&&) = default; 
+6
source

All Articles