1

All

wrote some fancy container with iterators. So I have

iterator begin() {
}

iterator end() {
}

const_iterator begin() const {
}

const_iterator end() const {
}

In test I instantiate container, fill it and testing it. What is an accepted/good way to call both const and non-const version? Say, simple test like

TEST( c.end() - c.begin() == c.size() );

should run for iterators as well as for const iterators. Const reference? Some ugly cast?

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64

2 Answers2

5

I would just make a const& to it:

const container& constC= c;
TEST(constC.end() - constC.begin() == constC.size());
David
  • 27,652
  • 18
  • 89
  • 138
1

I think not overloading them is a better solution as per std convention.

Declare cbegin() and cend() instead.

Dafang Cao
  • 897
  • 5
  • 15
  • 3
    the std convention is to do all of the above: `iterator begin();`, `const_iterator begin() const;`, and `const_iterator cbegin() const;` – David Jul 29 '16 at 01:16