【发布时间】:2012-11-19 14:51:58
【问题描述】:
考虑以下 C++03 程序:
#include <iostream>
struct T
{
mutable int x;
T() : x(0) {}
};
void bar(int& x)
{
x = 42;
}
void foo(const T& t)
{
bar(const_cast<int&>(t.x));
}
int main()
{
T t;
foo(t);
std::cout << t.x << '\n';
}
它appears to work,但它肯定安全吗?
我只是修改了一个 mutable 字段,但是完全剥离了它的 const 上下文让我很紧张。
【问题讨论】: