Re: [boost] [asio] some stuff about ipv4::address

----Original Message---- From: Chris Cleeland
On Fri, 13 Jan 2006, Christopher Kohlhoff wrote:
Some thoughts, both for and against:
- Does unsigned char always imply 0..255? No.
Might there be a standards-conforming C++ implementation where char is not 8 bits? Yes. Plausible other values are 9 (for 36-bit word addressed machines), and 32 (for DSPs where it is just too much heartache to slice a 32-bit word).
(Admittedly porting a sockets library, which inherently deals in sequences of octets, to this architecture could be rather difficult.) Not as bad as you think. Each octet must be able to fit into an unsigned char. It's just that an unsigned char can hold a value which doesn't fit in an octet.
Good question; I know that sizeof(char) == sizeof(unsigned char) == 1, but I don't know that it's guarantee anywhere that something that has a sizeof==1 is exactly 8 bits.
It isn't. It is defined that unsigned char has AT LEAST 8 bits (because it can hold 0..255). -- Martin Bonner Martin.Bonner@Pitechnology.com Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ, ENGLAND Tel: +44 (0)1223 203894

--- Martin Bonner <martin.bonner@pitechnology.com> wrote:
----Original Message---- From: Chris Cleeland
On Fri, 13 Jan 2006, Christopher Kohlhoff wrote:
Some thoughts, both for and against:
- Does unsigned char always imply 0..255? No.
So it seems that if asio is going to provide this constructor, then even if it uses unsigned char values there is the possibility (on some platforms) of out of range values. What should happen then if an out of range value is passed? The choice is between: - Silently changing the values to something in the range 0..255. - Throwing an exception (using boost::throw_exception of course, so that platforms that lack exceptions can use another way of handling the error). Whichever method is used, for a program to operate correctly it should range-check the values before passing them to the constructor. If a well-behaved program is going to do that anyway, what is the objection to using the exception approach? A well-behaved program will never trigger the exception. If you forget to range-check the values then you probably do want to know about it. Cheers, Chris

Christopher Kohlhoff wrote:
What should happen then if an out of range value is passed? The choice is between:
- Silently changing the values to something in the range 0..255.
Unacceptable, IMO.
- Throwing an exception (using boost::throw_exception of course, so that platforms that lack exceptions can use another way of handling the error).
- BOOST_ASSERT

On Sat, 14 Jan 2006, Christopher Kohlhoff wrote:
...for a program to operate correctly it should range-check the values before passing them to the constructor.
If a well-behaved program is going to do that anyway, what is the objection to using the exception approach? A well-behaved program will never trigger the exception. If you forget to range-check the values then you probably do want to know about it.
It seems that throwing the exception follows the principle of "fail early, and as close to the source of error as possible", which is, IMHO, good. I say throw the exception. -- Chris Cleeland, cleeland_c @ ociweb.com, http://www.milodesigns.com/~chris Principal Software Engineer, Object Computing, Inc., +1 314 579 0066 Support Me Supporting Cancer Survivors in Ride for the Roses 2005 >>>>>>>>> Donate at http://www.milodesigns.com/donate <<<<<<<<<

On 1/13/06 9:29 AM, "Christopher Kohlhoff" <chris@kohlhoff.com> wrote:
--- Martin Bonner <martin.bonner@pitechnology.com> wrote:
From: Chris Cleeland
On Fri, 13 Jan 2006, Christopher Kohlhoff wrote:
Some thoughts, both for and against:
- Does unsigned char always imply 0..255? No.
So it seems that if asio is going to provide this constructor, then even if it uses unsigned char values there is the possibility (on some platforms) of out of range values.
What should happen then if an out of range value is passed? The choice is between: [TRUNCATE]
Since this is only an issue if a byte is larger than an octet, maybe you should #if-block it off when CHAR_BIT == 8, so you don't perform the unnecessary test for octet-sized bytes. (C++, and C, is forbidden on platforms where a byte is smaller than an octet.) -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com

Christopher Kohlhoff wrote:
So it seems that if asio is going to provide this constructor, then even if it uses unsigned char values there is the possibility (on some platforms) of out of range values.
What should happen then if an out of range value is passed? The choice is between:
- Silently changing the values to something in the range 0..255.
- Throwing an exception (using boost::throw_exception of course, so that platforms that lack exceptions can use another way of handling the error).
Sorry for being soooooooo late to reply. Just got back from a month and a half vacation... It seems to me that the exception approach fits best to this case. I also like Daryle's idea of #ifdef-ing out the check when CHAR_BITS == 8, because the vast majority of Boost users do work in such environments, and would benefit from it. Yuval
participants (6)
-
Chris Cleeland
-
Christopher Kohlhoff
-
Daryle Walker
-
Martin Bonner
-
Peter Dimov
-
Yuval Ronen