fork(1) download
  1. #include <memory>
  2.  
  3. struct base { virtual ~base() {} };
  4. struct derived : public base {};
  5.  
  6. std::unique_ptr<base> f() {
  7. auto p = std::unique_ptr<derived>(new derived()); // std::make_unique<derived>();
  8. // p->foo();
  9. // works: return std::move(p);
  10. return p;
  11. }
  12.  
  13. int main() {
  14. auto x = f();
  15. return 0;
  16. }
Compilation error #stdin compilation error #stdout 0s 3024KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘std::unique_ptr<base> f()’:
prog.cpp:10:14: error: invalid conversion from ‘std::unique_ptr<derived>’ to ‘std::unique_ptr<derived>&&’ [-fpermissive]
In file included from /usr/include/c++/4.7/memory:86:0,
                 from prog.cpp:1:
/usr/include/c++/4.7/bits/unique_ptr.h:155:2: error:   initializing argument 1 of ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(std::unique_ptr<_Up, _Ep>&&) [with _Up = derived; _Ep = std::default_delete<derived>; <template-parameter-2-3> = void; _Tp = base; _Dp = std::default_delete<base>]’ [-fpermissive]
prog.cpp:10:14: error: cannot convert ‘p’ from type ‘std::unique_ptr<derived>’ to type ‘std::unique_ptr<derived>&&’
prog.cpp:11:5: warning: control reaches end of non-void function [-Wreturn-type]
stdout
Standard output is empty