4 ms·
Let's take this const map: const std::map<int, int> constMap = { {1,1}, {2,2 } }; Or take a const map copy-constructed from from a non-const map, if you wis
by jpz 9y ago
Let's take this const map:
const std::map<int, int> constMap = { {1,1}, {2,2 } };
Or take a const map copy-constructed from from a non-const map, if you wish.
How is this not immutable data, and "only provides a read-only view"?
It is linguistically guaranteed to be non-mutating - const-casts excepted of course.
All languages that are constructing const data types on the fly are putting them in a writeable pages in memory, after all.
- logicchains 9y ago>It is linguistically guaranteed to be non-mutating - const-casts excepted of course. Technically even after const-casting, if it was declared const then it's still not possible to mutate it, as doing so is undefined behaviour. "Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior." - http://en.cppreference.com/w/cpp/language/const_cast http://en.cppreference.com/w/cpp/language/const_cast It could for instance have been stored in non-writable memory, so any attempt to write to it will instantly segfault. This is one of my favourite things about C++ as people engaging in terrible programming practices like const-casting a const object get their just desserts, so to speak.