Avoid dereferencing in `operator->`.

std::to_address uses this function to "Obtain the address without forming a reference", so it might get surprised if it forms a reference in the process.
PiperOrigin-RevId: 595705426
pull/15268/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 06b8991ab1
commit b7a8a02ad9
  1. 16
      src/google/protobuf/repeated_field_unittest.cc
  2. 2
      src/google/protobuf/repeated_ptr_field.h

@ -23,6 +23,7 @@
#include <iterator>
#include <limits>
#include <list>
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
@ -55,6 +56,7 @@ namespace {
using ::protobuf_unittest::TestAllTypes;
using ::protobuf_unittest::TestMessageWithManyRepeatedPtrFields;
using ::testing::A;
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Ge;
@ -111,6 +113,20 @@ TEST(RepeatedPtrOverPtrsIterator, Traits) {
#endif
}
#if __cplusplus >= 202002L
TEST(RepeatedPtrOverPtrsIterator, ToAddress) {
// empty container
RepeatedPtrField<std::string> field;
EXPECT_THAT(std::to_address(field.pointer_begin()), A<std::string**>());
EXPECT_EQ(std::to_address(field.pointer_begin()),
std::to_address(field.pointer_end()));
// "null" iterator
using It = RepeatedPtrField<std::string>::pointer_iterator;
EXPECT_THAT(std::to_address(It()), A<std::string**>());
}
#endif
TEST(ConstRepeatedPtrOverPtrsIterator, Traits) {
using It = RepeatedPtrField<std::string>::const_pointer_iterator;
EXPECT_TRUE((std::is_same<It::value_type, const std::string*>::value));

@ -1727,7 +1727,7 @@ class RepeatedPtrOverPtrsIterator {
// dereferenceable
reference operator*() const { return *reinterpret_cast<Element*>(it_); }
pointer operator->() const { return &(operator*()); }
pointer operator->() const { return reinterpret_cast<Element*>(it_); }
// {inc,dec}rementable
iterator& operator++() {

Loading…
Cancel
Save