我有一个成员变量std::set<T*> m_associates;,即非常量原始指针的集合,并且只想检查是否存在另一个指针。为了保持 const 正确性,我的函数如下所示:
bool MyClass::is_associated(const T* x) const
{
return (m_associates.find(x) != m_associates.end());
}
但是,这不会编译,因为x传递 asconst T*表示指向的值x没有被函数更改,而是m_associates包含非常量T*。
如果我const从x参数中删除,它会编译,但违反 const 正确性......
添加const到m_associatesiestd::set<const T*> m_associates;也不是一个选项,因为我需要类中其他地方的非常量指针。
我该如何解决这个问题?这是(可能是唯一的)const_cast应该使用 a 的地方吗?还是我必须始终将所有参数T指针作为非常量传递?
编辑:完整的错误输出,编译器是 clang++-8,代码是 C++17
error: no matching member function for call to 'find'
return (m_associates.find(x) != m_associates.end());
~~~~~~~~~~~~^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_set.h:798:7: note: candidate function not viable: 1st argument ('const T *') would lose const qualifier
find(const key_type& __x) const
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_set.h:804:2: note: candidate function template not viable: 'this' argument has type 'const std::set<T *>', but method is not marked const
find(const _Kt& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_set.h:794:7: note: candidate function not viable: 'this' argument has type 'const std::set<T *>', but method is not marked const
find(const key_type& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_set.h:810:2: note: candidate template ignored: substitution failure [with _Kt = const T *]: no matching member function for call to '_M_find_tr'
find(const _Kt& __x) const
^