473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Casting and promotion

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
Jul 23 '05 #1
3 1757
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.


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

V
Jul 23 '05 #2
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
Jul 23 '05 #3

"BCC" <bc*@azed.net> skrev i en meddelelse
news:rC**************@newssvr13.news.prodigy.com.. .
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
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: BigMan | last post by:
Can someone cite the rules for type promotion in C++? And, in particular, what is the type of the result of adding 2 values of type char?
2
by: Andy | last post by:
Hi... i'm trying to understand the concept of function name overloading in c++. to understand the resolving system it's important to understand the diffrent levels of typecasting (exact match,...
10
by: Michael R. Copeland | last post by:
How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
21
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me....
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
23
by: PeterOut | last post by:
If I had code like this. unsigned short usLimit=10 int a, i; for (i=0; i<(int)usLimit; ++i) { a=(int)usLimit; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.