As far as I know in C++ struct/class members with the same access control are stored in memory in declaration order. Is next example m and c should be stored one after the other:
#include <cstdlib>
#include <iostream>
struct X
{
mutable int m;
int c;
};
const X cx = {0, 1};
int main()
{
X& x = const_cast<X&>(cx);
x.m = rand();
x.c = rand();
std::cout<<x.m<<" "<<x.c;
}
In this example the program runs and prints 2 random numbers. If I remove mutable it crashes because cx is stored in readonly protected memory.
This made me wonder - does one mutable member disable const optimizations for the entire struct(somehow make all members mutable)?
Is it possible to store parts of a struct in readonly memory and other parts on non-readonly memory and respect C++ standard memory layout?
This was tested using Visual Studio 2010 on Windows 7 and GCC 4.7.2 on Ubuntu.