0
  pointer operator->()
  {
    return ptr_+buffer_position_;
  }

  const_pointer operator->() const
  {
    return ptr_+buffer_position_;
  }

So the const overload returns a const pointer (yay) but the two functions do the same thing (boo) how does one avoid code duplication and the ensuing copypasta, while still maintaining const correctness?

(I'm looking at specifically C++98...though a C++11 answer would be pedagogic, as I suspect they did something to solve this problem).

IdeaHat
  • 7,192
  • 18
  • 47

1 Answers1

0

If you are not using c++11:

You can try this:

class T {
      pointer operator->()
      {
        return const_cast<pointer> (static_cast<const T*>(this)->operator->());
      }

      const_pointer operator->() const
      {
        return ptr_+buffer_position_;
      }
};
Daniel Heilper
  • 1,066
  • 1
  • 15
  • 31