Boost logo

Boost :

From: Chris Cleeland (cleeland_at_[hidden])
Date: 2006-01-12 10:17:12


On Fri, 13 Jan 2006, Christopher Kohlhoff wrote:

> Some thoughts, both for and against:
>
> - Does unsigned char always imply 0..255? Might there be a
> standards-conforming C++ implementation where char is not 8
> bits? (Admittedly porting a sockets library, which inherently
> deals in sequences of octets, to this architecture could be
> rather difficult.)

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.

That said, I can't think of any implementation anywhere that I've encountered
otherwise, including some pretty arcane old hardware architectures. As you
somewhat imply, porting code that deals in octets to such an architecture
would be pretty difficult, to say the least.

> - Let's assume you are using it for literal IP addresses, and
> the constructor takes 4 ints and throws an exception when out
> of range. If you do not pass out of range values the optimiser
> can determine that the values are valid, so there would be no
> performance impact.

I'm not sure I would agree that an exception is an appropriate response here,
but that might get into religious wars. If the interface provides an easy
opportunity to pass in out-of-range arguments, then somebody is going to do
it.

> - If the address was implemented as a C structure containing 4
> unsigned chars it could be initialised using { 1, 2, 3, 4 }.
> This constructor, if it took unsigned chars, would be the
> equivalent of that.

True.

> - Using unsigned chars doesn't just document the range of valid
> values, but also documents that an IP address is a 32-bit
> value composed of four 8-bit values.

I tried an experiment and, at least using g++ 4.0, I couldn't get the
*compiler* to complain when I had a CTOR that took 4 unsigned chars as args
and I passed in values that were out of range. So, maybe this idea is a nice
dream that can't make it to reality just yet.

-- 
  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    <<<<<<<<<

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk