4 ms·
const doesn't help most optimizing compilers, it only really helps prevent programmer clumsiness. Since most C/C++ compiler IR's are SSA-based all values are ef
by maximilianburke 15y ago
const doesn't help most optimizing compilers, it only really helps prevent programmer clumsiness. Since most C/C++ compiler IR's are SSA-based all values are effectively const anyways at the level optimizations are performed whether or not they're mutable in the original source code.
- excuse-me 15y agoYes it's probably only really useful for parallel code, but it's always nice to make constant things const.
- gte910h 15y agoWhy use "const int" there instead of #define? Far fewer issues with linkage and use in multiple files with #define.
- oldcigarette 15y agoNo. If it's const it is static be default. This is the kind of shit that makes the windows api the royal pain in the ass it is.
- gte910h 15y ago>If it's const it is static be default. If it's const at the file scope it's notably NOT static in the 'visible in the file scope only' sense of the word. Try it, declare a const int at file scope in a C file and use it in a different C file with a extern prototype.
- excuse-me 15y agoBecause you can do this at runtime. I might have a different value everytime I call the function but this means that inside the fucntion the compiler knows the value can't change
- gte910h 15y agoWhy ever use const at a file scope level. That's what I mean. #defines work SO much better there. Especially if being used outside the file.
- brigade 15y agoconst helps optimization in exactly one case: global data. Adding const there requires the value to be a compile-time constant, and guarantees the optimizer can inline the value rather than reading from memory. Though it still can inline the value in many cases if it can prove the variable isn't modified. Of course, this precludes conditional constructs such as the one proposed by excuse-me; that only works as a local var and the const is again irrelevant to the optimizer.