3 ms·
template<class T> struct const_ { const T value; }; const_<T>& freeze(T&); T& thaw(const_<T>&); do_something(const_<T>& x) { /* the language guarantees that
by gpderetta 10y ago
template<class T>
struct const_ { const T value; };
const_<T>& freeze(T&);
T& thaw(const_<T>&);
do_something(const_<T>& x)
{
/* the language guarantees that x can't be mutated from anywhere */
}
- pjmlp 10y agoYou can cast away const.
- gpderetta 10y ago'value' is a const qualified object, not a const qualified reference. If you cast away the const and write to the object you are into UB, ergo the compiler can assume that isn't done. Note that the trick still doesn't help much in practice: GCC conservatively still won't optimize based on const qualification of values.
- int_19h 10y agoNice. Of course, now you have to actually remember to write `const_<int> x = ...` instead of `const int x = ...` everywhere. And also get all the libraries to do the same...