3 ms·
this is totally wrong in my experience, although some of the understanding is right. by using static to limit to the compilation unit the compiler can workout t
by jheriko 5y ago
this is totally wrong in my experience, although some of the understanding is right. by using static to limit to the compilation unit the compiler can workout the value is constant.
const doesn't do this thanks to const_cast etc. maybe things have changed, but const on a file level variable doesn't do this reliably, or at least hasn't for considerable lengths of time.
of course 'static const' is the better answer. :)
- codeflo 5y agoI hope your comment wasn't intended to be as hostile as it reads to me, but I do wonder what your experience was exactly, because there might be a misinterpretation. From a technical standpoint, using const_cast to modify a constant is Undefined Behavior. This was explicitly specified that way to make constants inlineable by the compiler. Every compiler that I've ever used does this, it's a trivial but very effective optimization. Proof by Godbolt: https://godbolt.org/z/djEdvee4s https://godbolt.org/z/djEdvee4s Observe how GCC completely eliminates the contents of undefined_mutate_modulus (which it's allowed to -- UB means it can do anything with that function, and it chooses the simplest possible thing) rather than de-optimizing mod4_const like you suggested. Compilers are smart.