fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. namespace noncopyable_ // protection from unintended ADL
  5. {
  6.  
  7. ////////////////////////////////////////////////////////////////////////////
  8. // class noncopyable
  9. ////////////////////////////////////////////////////////////////////////////
  10.  
  11. class noncopyable
  12. {
  13. protected:
  14.  
  15. ////////////////////////////////////////////////////////////////////////
  16. // Construction/Destruction
  17.  
  18. noncopyable() {}
  19. ~noncopyable() {}
  20.  
  21. private:
  22.  
  23. // The following members are private
  24.  
  25. noncopyable(const noncopyable& that);
  26. const noncopyable& operator=(const noncopyable& that);
  27. };
  28.  
  29. } // namespace noncopyable_
  30.  
  31. typedef noncopyable_::noncopyable noncopyable;
  32.  
  33. class Test : private noncopyable
  34. {
  35. public:
  36. Test() : m_val() {}
  37. //Test(const Test& other) : m_val(other.m_val) {}
  38. ~Test() {}
  39.  
  40. private:
  41. int m_val;
  42. };
  43.  
  44. int main()
  45. {
  46. Test t1;
  47. Test t2(t1);
  48. // your code goes here
  49. return 0;
  50. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:47:15: error: use of deleted function 'Test::Test(const Test&)'
     Test t2(t1);
               ^
prog.cpp:33:7: note: 'Test::Test(const Test&)' is implicitly deleted because the default definition would be ill-formed:
 class Test : private noncopyable
       ^
prog.cpp:25:9: error: 'noncopyable_::noncopyable::noncopyable(const noncopyable_::noncopyable&)' is private
         noncopyable(const noncopyable& that);
         ^
prog.cpp:33:7: error: within this context
 class Test : private noncopyable
       ^
stdout
Standard output is empty