fork download
  1. #include <exception>
  2. #include <iostream>
  3. #include <type_traits>
  4.  
  5. class Moveable {
  6. static int s_count;
  7. int m_id;
  8. const char* m_ptr;
  9. public:
  10. Moveable(const char* ptr) : m_id(s_count++), m_ptr(ptr) {
  11. std::cout << "Moveable(ptr) " << m_id << '\n';
  12. }
  13.  
  14. Moveable(Moveable&& rhs) : m_id(s_count++), m_ptr(nullptr) {
  15. std::cout << "Moveable(&&) " << m_id << " from " << rhs.m_id << '\n';
  16. std::swap(m_ptr, rhs.m_ptr);
  17. }
  18.  
  19. ~Moveable() noexcept(false) {
  20. std::cout << "Release " << m_id << " m_ptr " << (void*)m_ptr << '\n';
  21. }
  22.  
  23. Moveable(Moveable const &) = delete;
  24. Moveable &operator=(Moveable const &) = delete;
  25. };
  26.  
  27. int Moveable::s_count;
  28.  
  29. int main(int argc, char *argv[]) {
  30. Moveable moveable{"hello world"};
  31. Moveable moved{std::move(moveable)};
  32. Moveable moved_again{std::move(moved)};
  33. }
  34.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Moveable(ptr) 0
Moveable(&&) 1 from 0
Moveable(&&) 2 from 1
Release 2 m_ptr 0x8048a26
Release 1 m_ptr 0
Release 0 m_ptr 0