The important thing here is that overload resolution is only done based on the arguments of the function, not the result. In your particular case, you have two different overloads, and the difference is that the implicit this is constant in one of them, that the overload will be collected whenever the static type of the object or the reference to which the method is called are constant.
If you want to force send a constant overload, you can get a link to a constant and then call this link:
btree<char> const & r = myTree; btree<char>::const_iterator it = r.find('M');
You should avoid this construct in real code, even if you use it for testing. The reason is that const and non-const overloads should have the same semantics, and therefore the behavior should be the same.
Also note that in standard containers there is an implicit conversion from iterator to const iterator to support the use of const_iterator directly in containers that do not contain constants. You must do the same, that is, if you provide an implicit conversion from iterator to const_iterator , then you can simply write:
btree<char>::const_iterator it = myTree.find('M');
... and it will work (it will not check the find method, but will allow you to check the behavior of const_iterator )
David RodrΓguez - dribeas
source share