A
Alf P. Steinbach
* Jacob:
There are at least three reasons why it's a Good Idea (TM) to use the C++
named casts also for simple built-in types:
* It helps you and maintainers of your code remember to not use C-style
casts in general, which can help to prevent accidental usage.
* C-style casts can do const_cast and reinterpret_cast, which you probably
don't want (especially not for pointer values).
* The type definitions may be changed when the code is maintained.
It is a common recommendation to use
"static_cast<SomeType> someInstance" instead of the
traditional infamous "(SomeType) someInstance".
Should I use the same practice for simple types,
i.e. instead of:
double fraction = (double) n / total;
should I write this?
double fraction = static_cast<double>(n) /
static_cast<double>(total);
I find the traditional version a lot more readable
in this case.
There are at least three reasons why it's a Good Idea (TM) to use the C++
named casts also for simple built-in types:
* It helps you and maintainers of your code remember to not use C-style
casts in general, which can help to prevent accidental usage.
* C-style casts can do const_cast and reinterpret_cast, which you probably
don't want (especially not for pointer values).
* The type definitions may be changed when the code is maintained.