On 14/03/2011 11:48, Leigh Johnston wrote:
On 14/03/2011 01:41, Paul wrote:
On 14/03/2011 00:09, Paul wrote:
On 13/03/2011 20:43, Paul wrote:
On 13/03/2011 19:29, Alf P. Steinbach /Usenet wrote:
* William Ahern, on 13.03.2011 03:01:
"If you stick to (unsigned int) or wider, then you're fine" is
generally
false. Use hammer for nails, screwdriver for screws. In short,
use the
right tool for the job, or at least don't use a clearly
inappropriate
tool: don't use signed types for bitlevel stuff, and don't use
unsigned
types for numerical stuff.
Bullshit. Using unsigned integral types to represent values that
are
never negative is perfectly fine. std::size_t ensures that the
C++
horse has already bolted as far as trying to avoid them is
concerned.
unsigned types can be safer because everything about the
arithmetic is well defined, including over- and underflows
which
occur
modulo 2^N; as opposed to signed, where those scenarios are
undefined.
The well-definedness of operations that you're talking about is
this:
that the language *guarantees* that range errors for unsigned
types
will
not be caught.
A guarantee that errors won't be caught does not mean "safer".
That's plain idiocy, sorry.
Plain idiocy is eschewing the unsigned integral types in C++.
Perhaps
you would prefer being a Java programmer? Java has less types to
"play
with" which perhaps would suit you better if you cannot cope with
C++'s richer set of types.
As Java, like C++, supports UDT's I don't think it's correct to
say
that
C++ suports a richer set of types.
class anyTypeYouLike{};
This is a troll. If is obvious I am referring to the set of
built-in
types. A user defined type is often composed of one or more
built-in
types.
There is a reason Java doesn't bother with a built in unsigned
numeric
type.
I think the people who created Java know more about programming
than you
do and it is not a case of Java being inadequete. This is just
your
misguided interpretation in an attempt to reinforce your idea that
std::size_t is the only type people should use in many
circumstances.
You obviously think std:size_t is the best thing since sliced
bread
and
this is the way forward in C++ and, as per usual, your opinion is
wrong.
C++'s std::size_t comes from C's size_t. It is the way forward
because
it is also the way backward.
The message you replied to ALf said to use the correct tool for
the
job,
which seems like a reasonable opinion. You replied saying this was
bullshit and implied Alf had said something about never using
unsigned,
your post looks like a deliberate attempt to start a flare.
You also make the point of saying using unsigned for values that
are
never negative is fine, but how do you know it is never going to
be
negative? Your idea of never negative is different from others',
you
think array indexes cannot be negative, but most other people know
they
can be.
If array indexes can be negative then please explain why
std::array::size_type will be std::size_t and not std:
trdiff_t.
Just because that particular array type cannot have a negative index
doesn't mean this applies to all arrays.
Array indexes *can* be negative see:
http://www.boost.org/doc/libs/1_46_1/libs/multi_array/doc/user.html
From n3242:
"if E1 is an array and E2 an integer, then E1[E2] refers to the E2-th
member of E1"
E2 cannot be negative if the above rule is not to be broken.
The above says E2 is an integer not an unsigned integer.
Also:
int arr[5] = {0};
arr[4] =5; /*The 5th element not the 4th*/
arr[0] = 1; /*The 1st elelment, not the 0th*/
int*p_arr = &arr;
++p_arr;
p_arr[-1] = 1; /*The 1st element, not the -1st*/
p_arr[0] = 2; /*The 2nd element, not the 0th*/
In C++ an array index can't be negative; p_arr above is not an array it
is a pointer. Obviously "E2-th" is zero-based not one-based. The fact
that Boost provides an array-like container which accepts negative
indices is mostly irrelevant; my primary concern is C++ not Boost;
again:
From n3242:
"if E1 is an array and E2 an integer, then E1[E2] refers to the E2-th
member of E1"
if E2 is negative then E1[E2] cannot possibly refer to a member of E1.
Also from n1336:
"if E1 is an array object (equivalently, a pointer to the initial
element of an array object) and E2 is an integer, E1[E2] designates the
E2-th element of E1 (counting from zero)."
This more closely matches my thinking on this subject; a sub-array
reference that allows you to give a negative array index on most
implementations is not an array object.