Very simple matrix template. Should your two operator() calls have the
same body? What if you later want to introduce bounds checking and
throw a range_error if the indices are out of bounds. We could add a
validate_bounds() command but we'd still have to call it from both
So the question is (probably the OPs question), should we get one
overload to call the other (or call a common function) using a
const_cast to override the constness. If so, how?
Is there a general way to resolve the problem whilst maintaining strict
constness, i.e. a way that would work without any const-casts and would
break (at compile time) if the const version really did modify anything
(other than a mutable).
Noah Roberts wrote:
> It makes more sense to go the other way; make the non-const version
> call the const version. Of course, the assumption is that you are
> returning the same value but possibly doing something on the side when
> not const. Chances are your in poor design mode but whatever...make
> the non-const version call the const version. Then you can't run into
How does that make more sense? Wouldn't that result in undefined
behavior when the client then modifies the returned int?
> > Does the above cause undefined behavior?
> No, but your getting close to causing UB. The above code also has
> pointless functions...you don't need the non-const version at all.
Heh... I don't need the non-const version because it is never called it
in the snippet I provided. The implication is that I need both the
non-const and the const versions.