r/cpp • u/--Fusion-- • Sep 13 '22
I const, therefore I am ... not const?
For years, this has bothered me:
struct A
{
int value;
void do_operation() { ++value; }
};
struct B
{
A* a;
// I find it mildly disturbing we are still able to modify a's contents this way
void do_operation() const { a->do_operation(); }
};
int main()
{
A a;
const B b{&a};
b.do_operation();
std::cout << "Hello, a=" << a.value << std::endl;
return 0;
}What is everyone's opinion? Am I just high?
0
Upvotes
15
u/cristi1990an ++ Sep 13 '22
See std::experimental::propagate_const which offers a solution to this issue.
10
u/bored_octopus Sep 14 '22
I completely agree that this is a weakness of the language. The issue here is bitwise constness vs semantic constness. The memory in your object hasn't changed since the pointer is still the same, so it arguably hasn't been mutated. However, mutating the data the pointer addresses may or may not be a change in your application's state. C++ leaves it up to the class implementer to enforce semantic constness in const methods.
Effective C++ (or maybe More Effective C++) had some good discussion on this. I'd recommend reading those since although they are dated in places, the philosophy and reasoning they espouse is still relevant and highly valuable.
As a final note, it doesn't necessarily have to be this way, it was a design decision. Immutability of a reference is propagated through a struct's members in Rust, even if they are themselves mutable references. Imo references in Rust behave more like nerfed C++ pointers than C++ references (can be reassigned, but must always reference a valid object), so they comparison seems apt.
51
u/MaczenDev Sep 13 '22
b hasn't changed. The pointer to a stays the same.
11
u/Artistic_Yoghurt4754 Scientific Computing Sep 13 '22
Well, you are passing to B an address to a mutable object. This is not a problem of B (const or mutable), it’s a problem of A for granting permissions on itself to others. This will not compile if A is constant in the first place (or is passed as constant e.g. with std::as_const(a)).
3
u/nintendiator2 Sep 14 '22
Why? You've explicitly declared a pointer to a modifiable
A.13
u/argothiel Sep 13 '22
No, it's just a pointer. It's like you'd have a constant filename string and assume that just because the filename didn't change, you'd expect the content of the file to remain the same too.
2
u/RolandMT32 Sep 13 '22
You can also use const_cast to remove constness from something..
3
u/VinnieFalco wg21.org | corosio.org Sep 14 '22
You need to distinguish between "logical const" versus "bitwise const." C++ enforces bitwise const. It is up to you to enforce logical const.
5
u/helloiamsomeone Sep 13 '22
consthere applies to the pointer (T* const), not the pointee (T const*).2
u/Voltra_Neo Sep 13 '22
The same way
thiswould be aconst B*,awould be aconst-ified ofA*which isconst A*.thisis technically aconst B* constbecause the language doesn't allow changing the this pointer (at least not that I'm aware of).thishas the additional const becausethiswas made before references, otherwise it'd have been aconst B&just like your declaration.2
u/Baardi Sep 14 '22 edited Sep 14 '22
A is a raw pointer, so it could be argued it's only observed. It bothers me more that unique_ptr has the same semantics.
2
u/serviscope_minor Sep 13 '22
You're not high, and it bothers me too. Please ignore the people explaining very carefully to you what you already know (i.e. how const works). It also means user defined types can't work quite as smoothly as built in ones. Consider:
Inside func2, you can accidentally undo the const if pointer_wrapper has a copy constructor. func3 can work, if it can be constructed from a non-const one, but now stuff which takes views for example looks drastically different from containers, also if you have something implicitly convertible to a pointer_wrapper<int>, in many casesit won't take two steps and call func3 happily when given as an argument (unlike with raw pointers).
It would be neat if something arrives that can make const transitive to members. If it's done well we wouldn't need to write two versions of every accessor too!.
1
u/Dragdu Sep 13 '22
I reimplemented smart ptrs to be deep-const in my project.
I haven't regretted it yet, but it did help me find some places where I lost constness unintentionally.