 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joe Bentley Guest
|
Posted: Wed Mar 03, 2004 11:44 am Post subject: type conversions: long to int in a 64-bit world |
|
|
What is the standard approach to convert a long to an int, realizing,
of course that you may not end up with the equivalent number? The
long values use to take up 4 bytes, now they're 8.
long L;
int I;
....
I = L;
Should this be a cast, or is there an acceptable practice to do this?
Will bitset help? Got an example?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Wed Mar 03, 2004 8:19 pm Post subject: Re: type conversions: long to int in a 64-bit world |
|
|
"Joe Bentley" <sweetiebentley (AT) hotmail (DOT) com> wrote:
| Quote: | What is the standard approach to convert a long to
an int, realizing,
of course that you may not end up with the
equivalent number?
|
How about this:
#include <stdexcept>
#include <limits>
int to_int(long n)
{
using std::numeric_limits;
if (n<static_cast
numeric_limits<int>::min())
| Quote: | | n>static_cast<long>(
numeric_limits<int>::max())) { |
throw std::range_error("to_int(long)");
}
return static_cast<int>(n);
}
Best regards,
Tom
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Mar 03, 2004 8:24 pm Post subject: Re: type conversions: long to int in a 64-bit world |
|
|
In message <e7ccda02.0403021548.6c00dc73 (AT) posting (DOT) google.com>, Joe
Bentley <sweetiebentley (AT) hotmail (DOT) com> writes
| Quote: | What is the standard approach to convert a long to an int, realizing,
of course that you may not end up with the equivalent number? The
long values use to take up 4 bytes, now they're 8.
|
No. The standard effectively requires 32 bits minimum for a long int but
it has never stipulated more than a minimum range. If you compiler
happens to use 64 bits that is a time independent decision.
Now before considering how to convert long to int consider whether it is
necessary. That is very important because what you are proposing to do
will destroy portability, it will assume that int provides at least 32
bits of storage. That is a dangerous and unnecessary assumption.
| Quote: |
long L;
int I;
...
I = L;
Should this be a cast, or is there an acceptable practice to do this?
Will bitset help? Got an example?
|
Got an example of why you want to do this? Messing with another
programmers source code by changing the choice of types needs very
serious justification.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Mar 03, 2004 8:28 pm Post subject: Re: type conversions: long to int in a 64-bit world |
|
|
Joe Bentley wrote:
| Quote: | What is the standard approach to convert a long to an int, realizing,
of course that you may not end up with the equivalent number? The
long values use to take up 4 bytes, now they're 8.
long L;
int I;
...
I = L;
Should this be a cast, or is there an acceptable practice to do this?
Will bitset help? Got an example?
|
It is impossible to say what is right or wrong, whithout knowing the
context. Why don't you leave the long as a long in the first place or
convert all such longs to ints? Those are the questions that have to be
answered first.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Mar 03, 2004 8:44 pm Post subject: Re: type conversions: long to int in a 64-bit world |
|
|
Joe Bentley wrote:
| Quote: | What is the standard approach to convert a long to an int, realizing,
of course that you may not end up with the equivalent number? The
long values use to take up 4 bytes, now they're 8.
|
That doesn't change the fact that there's a standard conversion
from long to int.
| Quote: | long L;
int I;
...
I = L;
Should this be a cast, or is there an acceptable practice to do this?
|
I don't see any point in using a cast. I recommend that you check
that a value is in the range of the type you are converting to,
whenever that range might not include the full range of the source
type.
| Quote: | Will bitset help? Got an example?
|
Whatever has bitset got to do with this?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Clark Cox Guest
|
Posted: Wed Mar 03, 2004 8:51 pm Post subject: Re: type conversions: long to int in a 64-bit world |
|
|
In article <e7ccda02.0403021548.6c00dc73 (AT) posting (DOT) google.com>,
[email]sweetiebentley (AT) hotmail (DOT) com[/email] (Joe Bentley) wrote:
| Quote: | What is the standard approach to convert a long to an int, realizing,
of course that you may not end up with the equivalent number? The
long values use to take up 4 bytes, now they're 8.
long L;
int I;
...
I = L;
Should this be a cast, or is there an acceptable practice to do this?
Will bitset help? Got an example?
|
Converting a long to an int on a 64-bit platform is the same as
converting it in any other platform. If the value is representable in
both types, then they will have the same value, if the value is not
representable in both types, then the result is implementation defined.
If you care about the value, check that it will fit in an int before you
do the conversion and take appropriate action if it will not.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Mar 04, 2004 10:02 pm Post subject: Re: type conversions: long to int in a 64-bit world |
|
|
Hi,
Francis Glassborow wrote:
| Quote: | Got an example of why you want to do this?
|
Because one uses two different libraries where the same business type
uses two different implementation types and the value needs to be
interfaced between the two?
Or because the source of data is a third-party database or some
communication protocol (long used) and the data sink uses different type
(int)?
We do not always have the control over what types are used.
Coercing types from smaller to bigger is not a problem, but relying on
"implementation defined behavior" in the reverse case is not always
viable. Controlled casting seems to be a good solution and "out of
range" error reporting can be a basic form of data validation.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tom Guest
|
Posted: Fri Mar 05, 2004 10:27 am Post subject: Re: type conversions: long to int in a 64-bit world |
|
|
[email]sweetiebentley (AT) hotmail (DOT) com[/email] (Joe Bentley) wrote in message news:<e7ccda02.0403021548.6c00dc73 (AT) posting (DOT) google.com>...
| Quote: | What is the standard approach to convert a long to an int, realizing,
of course that you may not end up with the equivalent number? The
long values use to take up 4 bytes, now they're 8.
long L;
int I;
...
I = L;
Should this be a cast, or is there an acceptable practice to do this?
Will bitset help? Got an example?
|
You should probably use a boost::numeric_cast, since this throws an
exception if the cast isn't valid (e.g. the value of L won't fit in
I). See www.boost.org.
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|