Skip to content

Commit 0d64fef

Browse files
Make value_type a non-const StringMapEntry<ValueTy>.
1 parent 2076520 commit 0d64fef

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

llvm/include/llvm/ADT/StringMap.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,15 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
432432
};
433433

434434
template <typename ValueTy, bool IsConst> class StringMapIterBase {
435-
using EntryTy = std::conditional_t<IsConst, const StringMapEntry<ValueTy>,
436-
StringMapEntry<ValueTy>>;
437-
438435
StringMapEntryBase **Ptr = nullptr;
439436

440437
public:
441438
using iterator_category = std::forward_iterator_tag;
442-
using value_type = EntryTy;
439+
using value_type = StringMapEntry<ValueTy>;
443440
using difference_type = std::ptrdiff_t;
444-
using pointer = value_type *;
445-
using reference = value_type &;
441+
using pointer = std::conditional_t<IsConst, const value_type *, value_type *>;
442+
using reference =
443+
std::conditional_t<IsConst, const value_type &, value_type &>;
446444

447445
StringMapIterBase() = default;
448446

@@ -453,8 +451,8 @@ template <typename ValueTy, bool IsConst> class StringMapIterBase {
453451
AdvancePastEmptyBuckets();
454452
}
455453

456-
reference operator*() const { return *static_cast<EntryTy *>(*Ptr); }
457-
pointer operator->() const { return &operator*(); }
454+
reference operator*() const { return *static_cast<value_type *>(*Ptr); }
455+
pointer operator->() const { return static_cast<value_type *>(*Ptr); }
458456

459457
StringMapIterBase &operator++() { // Preincrement
460458
++Ptr;

llvm/unittests/ADT/StringMapTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,19 @@ TEST(StringMapCustomTest, NonConstIterator) {
701701
static_assert(
702702
std::is_same_v<decltype(Map.begin()), StringMap<int>::iterator>);
703703

704+
// Check that the value_type of a non-const iterator is not a const type.
705+
static_assert(
706+
!std::is_const_v<StringMap<int>::iterator::value_type>,
707+
"The value_type of a non-const iterator should not be a const type.");
708+
709+
// Check that pointer and reference types are not const.
710+
static_assert(
711+
std::is_same_v<StringMap<int>::iterator::pointer,
712+
StringMap<int>::iterator::value_type *>);
713+
static_assert(
714+
std::is_same_v<StringMap<int>::iterator::reference,
715+
StringMap<int>::iterator::value_type &>);
716+
704717
// Check that we can construct a const_iterator from an iterator.
705718
static_assert(std::is_constructible_v<StringMap<int>::const_iterator,
706719
StringMap<int>::iterator>);
@@ -718,6 +731,19 @@ TEST(StringMapCustomTest, ConstIterator) {
718731
static_assert(std::is_same_v<decltype(ConstMap.begin()),
719732
StringMap<int>::const_iterator>);
720733

734+
// Check that the value_type of a const iterator is not a const type.
735+
static_assert(
736+
!std::is_const_v<StringMap<int>::const_iterator::value_type>,
737+
"The value_type of a const iterator should not be a const type.");
738+
739+
// Check that pointer and reference types are const.
740+
static_assert(
741+
std::is_same_v<StringMap<int>::const_iterator::pointer,
742+
const StringMap<int>::const_iterator::value_type *>);
743+
static_assert(
744+
std::is_same_v<StringMap<int>::const_iterator::reference,
745+
const StringMap<int>::const_iterator::value_type &>);
746+
721747
// Check that we cannot construct an iterator from a const_iterator.
722748
static_assert(!std::is_constructible_v<StringMap<int>::iterator,
723749
StringMap<int>::const_iterator>);

0 commit comments

Comments
 (0)