BCC wrote:
If I have:
double x = 0.0;
std::vector<std::string> local_vector;
local_vector.push_back("One!");
local_vector.push_back("Two!");
I am looking at some code from a colleague that does this all over the
place:
x = static_cast<double>( local_vector.size() );
Is this useful? I know that an int (or size_t) is promoted to a double
in this case. Is the static_cast expensive? It seems like a waste of
keystrokes and time to me... but I wasnt 100% sure.
Fix it or hit the silk. The code you show betrays dangerously bad
comprehension of many aspects of safe C++ coding.
A double's job is to represent an analog value, so that 24.9999 and 25.0 are
equivalent. Think of a thermometer. The difference in warmth between 24.9
and 25.0 degrees centigrade is irrelevant to humans, and to most chemical
reactions. However, the difference between 3 or 4 items is significant.
Next, if for some weird reason you need a double, you still don't need
static_cast, because the transition from an int to a double is considered a
transition from less to more precision. Sometimes it is not, which is why
you risk losing an item.
To fix it, get with both this code's author and a neutral third party, and
innocently ask "what's the static_cast" for. The best outcome would be a
group learning session, without making anyone feel put upon. If that's not
possible (if, for example, the code's author is your annointed Lead
Architect), then your project is in trouble...
--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand