fork download
  1. #include <map>
  2. #include <cstdio>
  3.  
  4. class NotCopyable
  5. {
  6. public:
  7. NotCopyable(char key):m_x(key){}
  8. NotCopyable(NotCopyable&& a)
  9. {
  10. printf("create %p from %p\n",this,&a);
  11. m_x=a.m_x;
  12. }
  13.  
  14. NotCopyable& operator=(NotCopyable&& a)
  15. {
  16. printf("move %p into %p\n",&a,this);
  17. return *this;
  18. }
  19. bool operator<(const NotCopyable& nc) const
  20. {return m_x<nc.m_x;}
  21. char m_x;
  22. };
  23.  
  24. int main()
  25. {
  26. std::map<NotCopyable,int> my_map;
  27. my_map.insert(std::pair<NotCopyable,int>('A',1));
  28. my_map.insert(std::pair<NotCopyable,int>('B',2));
  29. my_map.insert(std::pair<NotCopyable,int>('C',3));
  30.  
  31. auto i=my_map.begin();
  32. while(i!=my_map.end())
  33. {
  34. printf("'%c' => %d\n",i->first.m_x,i->second);
  35. NotCopyable foo=std::move( i->first );
  36. ++i;
  37. }
  38.  
  39. return 0;
  40. }
  41.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:35:39: error: use of deleted function ‘constexpr NotCopyable::NotCopyable(const NotCopyable&)’
   NotCopyable foo=std::move( i->first ); 
                                       ^
prog.cpp:4:7: note: ‘constexpr NotCopyable::NotCopyable(const NotCopyable&)’ is implicitly declared as deleted because ‘NotCopyable’ declares a move constructor or move assignment operator
 class NotCopyable
       ^
stdout
Standard output is empty