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( *((NotCopyable*)(&i->first)) );
  36. ++i;
  37. }
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
create 0x8171018 from 0xbfc0cd80
create 0x8171038 from 0xbfc0cd80
create 0x8171058 from 0xbfc0cd80
'A' =>  1
create 0xbfc0cd80 from 0x8171018
'B' =>  2
create 0xbfc0cd80 from 0x8171038
'C' =>  3
create 0xbfc0cd80 from 0x8171058