

C/C++ Low Level Curriculum Part 7: More Conditionals - marcieoum
http://www.altdevblogaday.com/2012/04/10/cc-low-level-curriculum-part-7-more-conditionals/

======
excuse-me
Another higher level and more useful use of the ?: operator is to set const
values

const int bytesPerPixel = haveAlphaChannel() ? 4 : 3;

This is useful for optimizating compilers - the more values in a function you
can make const the better it can do. And is especially useful for parallel
code like OMP/TBB

~~~
maximilianburke
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
Yes it's probably only really useful for parallel code, but it's always nice
to make constant things const.

~~~
gte910h
Why use "const int" there instead of #define? Far fewer issues with linkage
and use in multiple files with #define.

~~~
oldcigarette
No. 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
>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.

