What's the keyword mutable in the Moses source code for?

259 views Asked by At

This is about the source code of the statistical machine translation system Moses. In the Factor class of the Mosesdecoder project, there is this strange use of the keyword mutable:

class Factor {

    __SOME_OTHER_CODE__

    // FactorCollection writes here.
    // This is mutable so the pointer can be changed to pool-backed memory.
    mutable StringPiece m_string
    size_t m_id;

    __SOME_OTHER_CODE__
}

The complete file of Factor.h is here. I know that mutable is used when you want to modify a member variant in a const member function, or when you want to modify some out-of-scope variable in a lambda expression. I do not, however, understand what the mutable is doing in this code.

I appreciate any hint. Thank you.

1

There are 1 answers

3
Jerry Coffin On

You've provided little enough information that about all we can do is guess.

That said, from the comment, it sounds like they have some sort of storage pool, and they may want to move the storage for the string into the storage pool. Like most uses of mutable, it's to deal with a bitwise modification that still leaves the object logically un-modified (i.e., they change a pointer so it points to the same data, but at a different address).