Immutability is often considered to be a "best practice" in many programming languages. It reduces the likelihood of introducing unwanted side effe...
For further actions, you may consider blocking this person and/or reporting abuse
Impossible, eh? Strong words.
On a member function, the suffix const to a method declaration makes the implicit
this
argument itself const:That Foo::field() is really:
Since
this
is const, the field is also constant.So far, that's what you said. But C++ is well known for giving the programmer ample opportunity to shoot themselves in the foot.
One option is the const-cast:
Which is demonstrably evil. The developer has blown away the const across the whole object. Like all casts, this isn't a safe thing to do, and needs careful validation - but there are niche cases where it might be the only solution to a problem.
A more controlled mechanism is to use
mutable
:The
mutable
keyword prevents the effect of a constthis
pointer, but only for a particular field. Now the field can be changed legally within any const method. Which is, on the face of it, a Bad Thing.But there are a few legitimate cases for doing this. A good example is where you want to hold a lock during a read of a constant object. std::mutex::lock is a non-const method, so the
std::lock_guard
uses a non-const reference. All of which means this won't work:The solution is to make that mutex mutable - then the lock guard works and you're thread-safe.
But more importantly, while the method isn't "memory-const" anymore, it remains "semantically-const". To put it another way, unlike the previous examples I gave, this code behaves like you'd expect.
Oh, boiii. That looks pretty messy if you'd ask me. I just love how C++ can give you an infinite number of ways to shoot yourself in the foot. This is really funny, yet concerning. 😬
I wouldn't really count the
mutable
keyword since that's an actual feature of the language that allows you to "bypass" theconst
declaration.However, I would definitely count the crazy type-casting. That, right there, is why you have to love and hate C++ at the same time.
Thanks for sharing this! This is genuinely one of the more interesting comments I've seen in this site for a while.
Glad you found it interesting.
But the casts aren't inherently evil - they're a factor of C++ giving you all the tools you might need.
I was just looking for a reference about this yesterday. Bravo!
Dang, what are the odds of you coincidentally stumbling upon this article? That's amazing. 😁
Not much of one. I was poking around Dev this morning, thought "Huh, I wonder what new stuff Some Dood has written," and there it is!
It sounds so silly to say "I wonder what new stuff some dood has written" because of how vague and ambiguous it sounds when out of context. 😆 If someone outside the community read that, they would definitely ask what you meant by "some dood"? Ah, this is why I love this username. It just sounds so silly.
Irrelevant, but one social IRC room I hang out in is inhabited entirely by Python programmers. We maintain an entire gallery of statements that come up during conversation, but sound ridiculous out-of-context.
Yo, that sounds so funny. I'd love to read the humor of statements without context.
Anyway, we seem to be drifting quite far from your original comment. 😂 I wouldn't want to go too off-topic here. Thanks again for your nice comments! They really mean a lot.
Follow me back, and we can take the conversation over to PM, yeah?
GOOD IDEA! Why have I never thought about that before? I'll see you on the other side, my friend.
Thanks for the reminders!
Also of note: in many embedded systems const items stay in ROM, which provides runtime protection from bad people too ;)
This is the best advice which I've ever seen to remember these ambiguities in definition of
const
pointersThanks, man! All the credit goes to the guy who wrote that amazing StackOverflow answer.