Suppose you have a set of pointers (yeah...) :
std::set<SomeType*> myTypeContainer;
Then suppose that you want to search this set from a const method of SomeType:
bool SomeType::IsContainered() const
{
return myTypeContainer.find(this) != myTypeContainer.end();
}
This doesn't work. The this
ptr in the method is a const SomeType *const
, which I can't put into the find
. The issue being that find
takes a const-ref, which in this case would mean that the passed pointer is treated as const, but not the thing it points to.
Is there a way to resolve this smoothly (without changing the set template type)?
As you said, in the const member function this
becomes const SomeType *
(i.e. pointer to const), it can't be implicitly converted to SomeType *
(i.e. pointer to non-const), which is the expected parameter type of find
.
You could use const_cast
to perform explicit conversion.
bool SomeType::IsContainered() const
{
return myTypeContainer.find(const_cast<SomeType *>(this)) != myTypeContainer.end();
}
It would be safe if the cast result is not used for modifying; while std::set::find
won't do that.