================
@@ -330,7 +352,7 @@ class DenseMapBase : public DebugEpochBase {
   /// Range insertion of pairs.
   template <typename InputIt> void insert(InputIt I, InputIt E) {
     for (; I != E; ++I)
-      insert(*I);
+      try_emplace(I->first, I->second);
----------------
kazutakahirata wrote:

This change breaks range insertion with move iterators and move-only types:

```cpp
std::vector<std::pair<int, std::unique_ptr<int>>> V;
V.emplace_back(1, std::make_unique<int>(42));
llvm::DenseMap<int, std::unique_ptr<int>> M;
M.insert(std::make_move_iterator(V.begin()),
         std::make_move_iterator(V.end()));
```

With `std::move_iterator`, `*I` produces an rvalue reference, but `I->second` 
produces an lvalue reference, causing `try_emplace` to attempt a copy. Also, 
standard `InputIterator` only requires `*I` (and `operator->` on 
`std::move_iterator` is deprecated in C++20).

Could we keep `insert(*I)` in the range loop and instead add `insert(const 
BucketT &)` and `insert(BucketT &&)` overloads to `DenseMapBase`?

https://github.com/llvm/llvm-project/pull/221853
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to