不久前,我遇到了一些代码,它们用mutable关键字标记了一个类的成员变量。据我所知,它只是允许你在const方法中修改变量:

class Foo  
{  
private:  
    mutable bool done_;  
public:  
    void doSomething() const { ...; done_ = true; }  
};

这是唯一的使用这个关键字还是有更多的它比满足眼睛?从那以后,我在一个类中使用了这种技术,将boost::mutex标记为mutable,允许const函数出于线程安全的原因锁定它,但是,说实话,这感觉有点hack。


当前回答

它允许区分按位的const和逻辑的const。逻辑const是指对象没有以通过公共接口可见的方式更改,就像锁定示例一样。另一个例子是一个类,它在第一次请求时计算一个值,并缓存结果。

因为c++11 mutable可以用在lambda上,表示用value捕获的东西是可修改的(默认情况下不是):

int x = 0;
auto f1 = [=]() mutable {x = 42;};  // OK
auto f2 = [=]()         {x = 42;};  // Error: a by-value capture cannot be modified in a non-mutable lambda

其他回答

当你重写一个const虚函数并想要修改该函数中的子类成员变量时,mutable可以很方便。在大多数情况下,您不希望更改基类的接口,因此必须使用自己的可变成员变量。

使用boost::mutex正是这个关键字的目的。另一个用途是内部结果缓存,以加快访问速度。

基本上,'mutable'适用于任何不影响对象外部可见状态的类属性。

在你的问题中的示例代码中,mutable可能是不合适的,如果done_的值影响外部状态,它取决于…;部分。

它在有隐藏内部状态(如缓存)的情况下非常有用。例如:

class HashTable
{
...
public:
    string lookup(string key) const
    {
        if(key == lastKey)
            return lastValue;

        string value = lookupInternal(key);

        lastKey = key;
        lastValue = value;

        return value;
    }

private:
    mutable string lastKey, lastValue;
};

然后可以让一个const HashTable对象仍然使用它的lookup()方法,该方法修改内部缓存。

mutable关键字是一种戳穿对象上的const面纱的方法。如果你有一个指向对象的const引用或指针,你不能以任何方式修改该对象,除非它被标记为可变的。

使用const引用或指针,你被限制为:

仅对任何可见数据成员进行读访问 只调用标记为const的方法的权限。

可变异常使您现在可以编写或设置标记为可变的数据成员。这是唯一能从外部看到的区别。

在内部,那些对你可见的const方法也可以写入标记为可变的数据成员。本质上,const面纱是全面穿透的。完全由API设计者来确保mutable不会破坏const概念,并且只在有用的特殊情况下使用。mutable关键字有帮助,因为它清楚地标记了受这些特殊情况影响的数据成员。

在实践中,您可以在整个代码库中频繁地使用const(实际上您希望用const“疾病”“感染”代码库)。在这个世界中,指针和引用都是const的,只有极少数例外,从而产生的代码更容易推理和理解。一个有趣的题外话,请查阅“参考透明度”。

Without the mutable keyword you will eventually be forced to use const_cast to handle the various useful special cases it allows (caching, ref counting, debug data, etc.). Unfortunately const_cast is significantly more destructive than mutable because it forces the API client to destroy the const protection of the objects (s)he is using. Additionally it causes widespread const destruction: const_casting a const pointer or reference allows unfettered write and method calling access to visible members. In contrast mutable requires the API designer to exercise fine grained control over the const exceptions, and usually these exceptions are hidden in const methods operating on private data.

(注意:我多次提到数据和方法可见性。我说的是标记为public、private或protected的成员,这是这里讨论的完全不同类型的对象保护。)

Mutable主要用于类的实现细节。类的用户不需要知道它,因此他认为“应该”是const的方法可以是。让互斥量可变的例子是一个很好的规范示例。