 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jim Langston Guest
|
Posted: Tue Nov 21, 2006 10:10 am Post subject: static_cast<unsigned short)( -1 ) Well defined? |
|
|
Is the following well defined?
size_t IntVal = 65537;
unsigned short Length;
if ( IntVal > static_cast<unsigned short>( -1 ) )
{
std::cout << "Value too long to fit in a short" << std::endl;
}
else
{
std::cout << "Value fits" << std::endl;
Length = static_cast<unsigned short>( IntVal );
}
What I'm actually going to be using it for is to send a length short through
sockets, and I want to ensure that the length isn't greater than 65535.
Rather than the magic number, I was thinking that static_cast<unsigned
short>( -1 ) would be better. Is this well defined? Will it equal 65535 in
all cases where a short is a 2 byte integer? |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Tue Nov 21, 2006 10:10 am Post subject: Re: static_cast<unsigned short)( -1 ) Well defined? |
|
|
Jim Langston wrote:
| Quote: | Is the following well defined?
size_t IntVal = 65537;
unsigned short Length;
if ( IntVal > static_cast<unsigned short>( -1 ) )
{
std::cout << "Value too long to fit in a short" << std::endl;
}
else
{
std::cout << "Value fits" << std::endl;
Length = static_cast<unsigned short>( IntVal );
}
What I'm actually going to be using it for is to send a length short
through sockets, and I want to ensure that the length isn't greater than
65535.
|
If that is want you want to check, you should say so:
if ( IntVal > 65535 ) { ...
| Quote: | Rather than the magic number, I was thinking that
static_cast<unsigned short>( -1 ) would be better.
|
Why? If you want to check > 65535, then presumably, your specs contain a
magic number. Your code should reflect that.
| Quote: | Is this well defined?
|
Your code has implementation defined behavior. Moreover, it is guaranteed,
that for IntVal <= 65535, the "Value fits" branch will be taken.
| Quote: | Will it equal 65535 in all cases where a short is a 2 byte integer?
|
Yes, provided your bytes have eight bits. However, your bytes maybe longer
and your shorts may have more than 2 bytes.
Now, you need to distinguish whether you want to check
(a) whether IntVal <= 65536, or
(b) whether IntVal can be faithfully represented as a short unsigned.
Your code does the later, and as far as I can see, it does so correctly.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Tue Nov 21, 2006 10:10 am Post subject: Re: static_cast<unsigned short)( -1 ) Well defined? |
|
|
Jim Langston wrote:
| Quote: | Is the following well defined?
size_t IntVal = 65537;
unsigned short Length;
if ( IntVal > static_cast<unsigned short>( -1 ) )
{
std::cout << "Value too long to fit in a short" << std::endl;
}
else
{
std::cout << "Value fits" << std::endl;
Length = static_cast<unsigned short>( IntVal );
}
What I'm actually going to be using it for is to send a length short through
sockets, and I want to ensure that the length isn't greater than 65535.
Rather than the magic number, I was thinking that static_cast<unsigned
short>( -1 ) would be better. Is this well defined? Will it equal 65535 in
all cases where a short is a 2 byte integer?
|
I doubt that can be enforced or guarenteed other than by checking
dutifully- but i may be wrong.
take a look at:
template<>
struct numeric_limits<unsigned short>
in...
#include <limits>
namespace Project {
typedef unsigned short usocket;
};
int main()
{
using Project::usocket;
using std::numeric_limits;
std::cout << "numeric_limits< usocket >::min() ";
std::cout << numeric_limits< usocket >::min();
std::cout << std::endl;
std::cout << "numeric_limits< usocket >::max() ";
std::cout << numeric_limits< usocket >::max();
std::cout << std::endl;
int n(65530);
for(size_t t = 0; t < 10; ++t)
{
std::cout << "n++ = " << n++;
usocket usock(static_cast<usocket>(n));
std::cout << "\tusock = " << usock;
std::cout << std::endl;
}
}
/*
numeric_limits< usocket >::min() 0
numeric_limits< usocket >::max() 65535
n++ = 65530 usock = 65531
n++ = 65531 usock = 65532
n++ = 65532 usock = 65533
n++ = 65533 usock = 65534
n++ = 65534 usock = 65535
n++ = 65535 usock = 0
n++ = 65536 usock = 1
n++ = 65537 usock = 2
n++ = 65538 usock = 3
n++ = 65539 usock = 4
*/ |
|
| 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
|
|