假设你有一组指针(是的......):
std::set<SomeType*> myTypeContainer;
然后假设你想从 SomeType 的 const 方法中搜索这个集合:
bool SomeType::IsContainered() const
{
return myTypeContainer.find(this) != myTypeContainer.end();
}
这行不通。方法中的this
ptr 是 a const SomeType *const
,我不能放入find
. 问题是find
需要一个 const-ref,在这种情况下,这意味着传递的指针被视为 const,而不是它指向的东西。
有没有办法顺利解决这个问题(不改变设置的模板类型)?