This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/68869] New: map::insert(P&&) broken in some cases


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

            Bug ID: 68869
           Summary: map::insert(P&&) broken in some cases
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rs2740 at gmail dot com
  Target Milestone: ---

According to the standard ([map.modifiers]), 

template <class P> pair<iterator, bool> insert(P&& x);

is equivalent to return emplace(std::forward<P>(x)), provided that
std::is_constructible<value_type, P&&>::value is true.

The following code does not compile when insert() is used, but does compile
when emplace() is used. The same issue also affects multimap.

#include <map>
#include <utility>
#include <type_traits>
#include <memory>

struct S {
    operator std::pair<const int, int>() const && { return {}; }
};

static_assert(std::is_constructible<std::pair<const int, int>, S&&>::value,
"!!");
static_assert(std::is_constructible<std::pair<const std::unique_ptr<const int>,
int>, std::pair<std::unique_ptr<int>, int>&&>::value, "!!");

int main(){
    std::map<std::unique_ptr<const int>, int> m;
    std::map<int, int> m2;
    m.insert(std::make_pair(std::make_unique<int>(), 1));  // Error
    m2.insert(S());  // Error

    m.emplace(std::make_pair(std::make_unique<int>(), 1)); // OK
    m2.emplace(S()); // OK
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]