Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting and promotion

BCC
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Casting and promotion


BCC wrote:[color=blue]
> 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.[/color]

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

V


Phlip
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Casting and promotion


BCC wrote:
[color=blue]
> 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.[/color]

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


Peter Koch Larsen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Casting and promotion



"BCC" <bcc@azed.net> skrev i en meddelelse
news:rCfde.180$5o2.88@newssvr13.news.prodigy.com.. .[color=blue]
> 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[/color]
I agree with you. Its just plain silly and adds nothing but confusion.

/Peter


Closed Thread