【发布时间】:2011-04-30 23:06:13
【问题描述】:
考虑以下 sn-p:
#include <map>
class C {
public:
C() {}
const int& f(const int& x) const
{
// Error: cannot cast const int* to int* const
return myMap.find(&x)->second;
// With a const_cast works:
//return myMap.find(const_cast<int* const>(&x))->second;
}
std::map<int*, int> myMap;
};
int _tmain(int argc, _TCHAR* argv[])
{
int x = 0;
C c;
c.f(x);
return 0;
}
f() 中的错误是由于 map 的 find() 采用 const KeyType& 的 const 重载引起的。因为映射的键类型是int*,所以这就变成了int* const。 f() 带一个const int& 参数,这是正确的,因为该参数永远不会被修改。
不幸的是,这最终导致尝试将 const int* 强制转换为 int* const,这会丢失 int 上的 const 限定符并且无法编译。
这有点烦人,因为该参数绝对没有被修改过 - 它只是用于 find() - 但我仍然需要 const_cast 它。
有没有办法在没有const_cast 的情况下编写f()?
【问题讨论】:
-
这很不寻常:你为什么使用指针作为映射键?
-
在我的应用程序中,键需要是对重量级对象的引用,并且不能将引用用作映射键。
标签: c++ stl map const-correctness