[clang] [llvm] [mlir] [ADT] Give DenseMapPair its own members instead of a std::pair base. NFC (PR #221853)
Kazu Hirata via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 7 18:18:36 PDT 2026
================
@@ -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
More information about the cfe-commits
mailing list