https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85970

            Bug ID: 85970
           Summary: Cannot move a std::unique_ptr to insert into a map
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jose.dapena at lge dot com
  Target Milestone: ---

Created attachment 44206
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44206&action=edit
Test case

Trying to insert to an std::map with value type std::unique_ptr using a move to
the initializer list fails:

  std::map<std::string, std::unique_ptr<A>> map;

  std::string str = "a";
  std::unique_ptr<A> a = std::make_unique<A>();

  // This fails
  map.insert({str, std::move(a)});


But, if instead of this we do:

  map.insert(std::pair<std::string, std::unique_ptr<A>>(str, std::move(a)));

It works.

This is working in LLVM libc++.

The error we get is related to trying to use the copy contructor of std::pair
and std::unique_ptr:
/usr/include/c++/7/ext/new_allocator.h:136:4: error: use of deleted function
‘std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const
std::__cxx11::basic_string<char>; _T2 = std::unique_ptr<A>]’
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/7/bits/stl_tree.h:63,
                 from /usr/include/c++/7/map:60,
                 from main.cc:1:
/usr/include/c++/7/bits/stl_pair.h:292:17: note: ‘std::pair<_T1,
_T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const
std::__cxx11::basic_string<char>; _T2 = std::unique_ptr<A>]’ is implicitly
deleted because the default definition would be ill-formed:
       constexpr pair(const pair&) = default;
                 ^~~~
/usr/include/c++/7/bits/stl_pair.h:292:17: error: use of deleted function
‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with
_Tp = A; _Dp = std::default_delete<A>]’
In file included from /usr/include/c++/7/memory:80:0,
                 from main.cc:2:
/usr/include/c++/7/bits/unique_ptr.h:388:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^~~~~~~~~~

Reply via email to