473,379 Members | 1,533 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,379 software developers and data experts.

Do I need a typecast here?

Hi.

I heard that in C++ typecasts are "evil". However, what does one do
with this thing?:

---
/* Figure out how many digits of fraction we'll get.
* We add 1 to the lengths of a and b to take into account
* the hidden bits.
*/
int fracObtained(((aLength + 1) - (bLength + 1)));
std::size_t bufExtra(0);
if((fracObtained < rLength) || (fracObtained < MIN_PRECISION))
<---- yuck :(
{
/* We need more */
bufExtra = rLength - fracObtained;
}
---

But if I simply do

---
/* Figure out how many digits of fraction we'll get.
* We add 1 to the lengths of a and b to take into account
* the hidden bits.
*/
int fracObtained(((aLength + 1) - (bLength + 1)));
std::size_t bufExtra(0);
if(fracObtained < rLength) <--- nice :) but doesn't work due to
signed/unsigned comparison :(
{
/* We need more */
bufExtra = rLength - fracObtained;
}
---

it fails when "fracObtained" is negative, because rLength is also of
type std::size_t, which
is an unsigned type. The former code uses no typecast, the latter
doesn't either, but the
former works however it having that goofy extra check in there may be
confusing while
the latter, simply comparing it to rLength seems clearer and more
natural. But for the latter to work,
you need a typecast of rLength to int. But is the "evil" here
necessary if one wants to
write good code for this?
Feb 27 '08 #1
1 1504
On Feb 27, 10:44 pm, mike3 <mike4...@yahoo.comwrote:
I heard that in C++ typecasts are "evil".
Sometimes a necessary evil. And even...
However, what does one do with this thing?:
---
/* Figure out how many digits of fraction we'll get.
* We add 1 to the lengths of a and b to take into account
* the hidden bits.
*/
int fracObtained(((aLength + 1) - (bLength + 1)));
std::size_t bufExtra(0);
if((fracObtained < rLength) || (fracObtained < MIN_PRECISION))
<---- yuck :(
{
/* We need more */
bufExtra = rLength - fracObtained;
}
---
But if I simply do
---
/* Figure out how many digits of fraction we'll get.
* We add 1 to the lengths of a and b to take into account
* the hidden bits.
*/
int fracObtained(((aLength + 1) - (bLength + 1)));
std::size_t bufExtra(0);
if(fracObtained < rLength) <--- nice :) but doesn't work due to
signed/unsigned comparison :(
{
/* We need more */
bufExtra = rLength - fracObtained;
}
---
it fails when "fracObtained" is negative, because rLength is
also of type std::size_t, which is an unsigned type.
Comparing a signed integral type with an unsigned is evil in
C++. There will be an implicit conversion, but not necessarily
in the way you want.
The former code uses no typecast, the latter doesn't either,
but the former works however it having that goofy extra check
in there may be confusing while the latter, simply comparing
it to rLength seems clearer and more natural.
But for the latter to work, you need a typecast of rLength to
int. But is the "evil" here necessary if one wants to write
good code for this?
The evil is that you're mixing signed and unsigned integral
types:-). Depending on what you are doing, it may be an
unavoidable evil---you can't change the type of sizeof, and you
can't change the types returned by std::vector<>::size() and
others. If you can't avoid mixing signed and unsigned, then
telling the compiler (and the reader) explicity which way you
want the conversions to be done is good. (In many ways,
implicit conversions are even more evil than casts. But again,
it depends on the context.)

Of course, if there are no other constraints, the "correct"
solution is to make rLength an int. (In C++, int is the
default integral type, and should always be used for integral
values unless there is some constraint which makes it
impossible, or at least unreasonable.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 28 '08 #2

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

Similar topics

5
by: Lars Plessmann | last post by:
I have a problem with typecast methods. Here is a setter of an object: function setKind($kind) { if (is_int((int)$kind) and (strlen($kind)<=6)) { $this->kind = $kind; return true;
9
by: Venkat | last post by:
Hi All, I want to typecast int to std::string how can i do it. Here is the sample code. int NewList; //Fill the NewList with integers values. .......
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
8
by: Trishia Rose | last post by:
this is something ive always wondered, does it take cpu time at run time to typecast or just at compile time? for example consider the two little bits of code: int a = 5; int b = a; and: ...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
8
by: akolsen | last post by:
Hello there. This should be simple, but im having trouble anyway of getting it to work. I have a boxed object that i want to cast to its native type, but I would like to use reflection to do...
11
by: Manikandan | last post by:
Hi, I am using the following snippet to compare an object with integer in my script. if ( $forecast < 4 ) { I got the "segmentation fault" error message when i executed this script in CLI....
1
by: skumar22 | last post by:
when I typecast a float to an int. Does the compiler allocate an additional space? for eg if I do something like. float pi = 3.14; int i = (int) pi; then an additional space is created for...
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.