fork download
  1. #include <boost/noncopyable.hpp>
  2. #include <memory>
  3.  
  4.  
  5. class U {};
  6.  
  7. U* create_T() { return new U; }
  8.  
  9. class T : boost::noncopyable {
  10. public:
  11. explicit T(U *p) : p_(p) {
  12. }
  13.  
  14. ~T() {
  15. delete p_;
  16. }
  17.  
  18. operator bool() const { return p_ != 0; }
  19.  
  20.  
  21. private:
  22. mutable U *p_;
  23. U* release() const { U* r = p_; p_ = 0; return r; }
  24. friend class InitT;
  25. };
  26.  
  27. class InitT {
  28. public:
  29. T t;
  30. InitT(const InitT& o) : t(o.t.release()) {}
  31. InitT(U* u) : t(u) {}
  32. operator bool() const { return t; }
  33. };
  34.  
  35. int main() {
  36. T x = new U; // allowed
  37. T y(new U); // allowed
  38. std::auto_ptr<U> a = new U;
  39. std::auto_ptr<U> b(new U);
  40.  
  41. if (T i = create_T()) {}
  42. if (T i(create_T())) {}
  43. if (U* u = create_T()) { T i(u); }
  44. if (InitT i = create_T()) { i.t; }
  45.  
  46. //T z = x; // not allowed
  47. //T x2(x) // not allowed
  48. }
Compilation error #stdin compilation error #stdout 0.01s 2852KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:36: error: conversion from ‘U*’ to non-scalar type ‘T’ requested
prog.cpp:38: error: conversion from ‘U*’ to non-scalar type ‘std::auto_ptr<U>’ requested
prog.cpp:41: error: conversion from ‘U*’ to non-scalar type ‘T’ requested
prog.cpp:42: error: expected primary-expression before ‘i’
prog.cpp:42: error: expected `)' before ‘i’
prog.cpp:44: warning: statement has no effect
stdout
Standard output is empty