Let's say I have a magic number
I want to get rid off...
//whatever.cpp
for (int i = 0; i < 42; i++)
{
//...
}
Reasonably I could kill it in two ways:
Either with const int SOMETHING_SOMETHING_MEANING_OF_LIFE = 42
or with constexpr int SOMETHING_SOMETHING_MEANING_OF_LIFE = 42
in the source .cpp
file.
Is there any meaningful difference between the two in this case (I recall the compiler deducing that - in either case - that the value does not change and thus the 42
is actually hardcoded in the resulting loop/unrolled loop/whatever machine-code) or does it come down to personal taste?
In a related issue: what if the magic number
(and thus the thing that replaces it) were declared in a header (.h
) file instead of a source (.ccp
) file - would that change things (and if so, how)?