0

I have a C++ class field that is declared mutable. When it was of type double, I could modify it in a const method with no problem. But when I switched its type to a class in which operator= is overloaded, I got the error that this overloaded operator= does not take a const pointer to this. Is this a gcc bug, or a language quirk?

user410315
  • 696
  • 5
  • 8
  • 5
    Please show some code (as little as possible) that reproduces the problem ... and tell us what version of gcc you're using – Useless Oct 31 '12 at 16:26
  • Can't replicate neither with gcc 4.3.4 nor gcc 4.7.0: http://ideone.com/0mvlJi – Electro Oct 31 '12 at 16:33

2 Answers2

1

When you suspect a compiler bug you should say which compiler version you are using. You know, bugs are killed every day.

Anyway, this code compiles fine with GCC 4.6.3:

struct A
{
    void operator=(int)
    {
    }
};

struct B
{
    mutable A a;
};

int main()
{
    const B b;
    b.a = 42;
}

But, of course, this does not (it this your case):

struct A
{
    mutable int m;
    void operator=(int x)
    {
        m = x;
    }
};

struct B
{
    A a;
};

int main()
{
    const B b;
    b.a = 42;
}
rodrigo
  • 94,151
  • 12
  • 143
  • 190
0

mutable has the effect that const qualification on the implicit object parameter is nullified (5.2.5 Class member access [expr.ref], paragraph 4):

[...] If E2 is declared to be a mutable member, then the type of E1.E2 is “vq12 T”. If E2 is not declared to be a mutable member, then the type of E1.E2 is “cq12 vq12 T”.

5.2.5 applies for implicit member access per 9.3.1p3:

When an id-expression [...] is used in a member of class X [...] the id-expression is transformed into a class member access expression using (*this) as the postfix-expression to the left of the . operator.

So in your const method the mutable member is not (observed to be) const-qualified. Your observed behaviour must be the result of a compiler bug.

ecatmur
  • 152,476
  • 27
  • 293
  • 366