Add const version of operator[].

pull/14146/head
Mark D. Roth 7 years ago
parent 19688bf7a8
commit 9ab4d0c826
  1. 9
      src/core/lib/gprpp/inlined_vector.h
  2. 16
      test/core/gprpp/inlined_vector_test.cc

@ -61,6 +61,15 @@ class InlinedVector {
}
}
const T& operator[](size_t offset) const {
assert(offset < size_);
if (offset < N) {
return *reinterpret_cast<const T*>(inline_ + offset);
} else {
return dynamic_[offset - N];
}
}
template <typename... Args>
void emplace_back(Args&&... args) {
if (size_ < N) {

@ -86,6 +86,22 @@ TEST(InlinedVectorTest, ClearAndRepopulate) {
}
}
TEST(InlinedVectorTest, ConstIndexOperator) {
const int kNumElements = 10;
InlinedVector<int, 5> v;
EXPECT_EQ(0, v.size());
for (int i = 0; i < kNumElements; ++i) {
v.push_back(i);
EXPECT_EQ(i + 1, v.size());
}
auto const_func = [kNumElements](const InlinedVector<int, 5>& v) {
for (int i = 0; i < kNumElements; ++i) {
EXPECT_EQ(i, v[i]);
}
};
const_func(v);
}
} // namespace testing
} // namespace grpc_core

Loading…
Cancel
Save