fork(1) download
  1. #include <memory>
  2. #include <vector>
  3.  
  4. struct item {};
  5. int main() {
  6. std::vector<std::unique_ptr<item>> _v;
  7. _v.resize(100);
  8. // is it safe to use the assignment operator?
  9. _v[20] = new item;
  10. return 0;
  11. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:9:15: error: no match for ‘operator=’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<std::unique_ptr<item> > >::value_type {aka std::unique_ptr<item>}’ and ‘item*’)
  _v[20] = new item;
               ^~~~
In file included from /usr/include/c++/6/memory:81:0,
                 from prog.cpp:1:
/usr/include/c++/6/bits/unique_ptr.h:252:7: note: candidate: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = item; _Dp = std::default_delete<item>]
       operator=(unique_ptr&& __u) noexcept
       ^~~~~~~~
/usr/include/c++/6/bits/unique_ptr.h:252:7: note:   no known conversion for argument 1 from ‘item*’ to ‘std::unique_ptr<item>&&’
/usr/include/c++/6/bits/unique_ptr.h:272:2: note: candidate: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> >, std::__or_<std::__and_<std::is_reference<_Dp>, std::is_same<_T2, _U2> >, std::__and_<std::__not_<std::is_reference<_Dp> >, std::is_convertible<_Ep, _Dp> > > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = item; _Dp = std::default_delete<item>]
  operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  ^~~~~~~~
/usr/include/c++/6/bits/unique_ptr.h:272:2: note:   template argument deduction/substitution failed:
prog.cpp:9:15: note:   mismatched types ‘std::unique_ptr<_Tp, _Dp>’ and ‘item*’
  _v[20] = new item;
               ^~~~
In file included from /usr/include/c++/6/memory:81:0,
                 from prog.cpp:1:
/usr/include/c++/6/bits/unique_ptr.h:281:7: note: candidate: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = item; _Dp = std::default_delete<item>; std::nullptr_t = std::nullptr_t]
       operator=(nullptr_t) noexcept
       ^~~~~~~~
/usr/include/c++/6/bits/unique_ptr.h:281:7: note:   no known conversion for argument 1 from ‘item*’ to ‘std::nullptr_t’
stdout
Standard output is empty