Casting and promotion

B

BCC

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.

Thanks,
B
 
P

Phlip

BCC said:
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...
 
V

Victor Bazarov

BCC said:
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.

It's not expensive. It's superfluous. It's indeed a waste of keystrokes.

V
 
P

Peter Koch Larsen

BCC said:
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.

Thanks,
B
I agree with you. Its just plain silly and adds nothing but confusion.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top