ccl and stl: design differences

J

jacob navia

In the ValArrays of the C containers library slices are defined
for an array and then ALL operations (indexing, searching, assigning
etc) will operate only in the slice, as if the other elements did
not exist.

In the STL the slice class is used with the overloaded operator[ ]
to obtain essentially the same effect as in C. Besides the absence of
overloaded operator [ ] in normal C, in C++ you have to manually
adjust the slice if the underlying array changes, i.e. if you
add or delete elements the slice will index a different set of
elements.

The C version avoids this since any modifications to the array will
also modify the slice accordingly.

Another difference is the behavior of the Max/Min functions. In
C++ making Max(array) with an empty array provokes undefined
behavior, and any result is valid.

In the C version I have tried to avoid undefined behavior
completely with a detailed error analysis. In this case
Max and Min return the identity element for those operations:
Max returns the smallest element storable in the underlying ValArray
type, Min returns the biggest element since

Max(a,smallest) ==> a for all a
Min(a, biggest) ==> a for all a

This is better than the undefined behavior in C++ in my opinion:
gcc returns zero for Max(empty_array). Zero is a very common value.

Another difference is the behavior of the Mismatch function. In the C
version if the arrays are of different length the smallest length
will be used. Elements will be compared, and if there is a difference
before the end of the smallest array that is the result. If there is no
difference and the smallest array is exhausted the result will be
one more than the length of the smallest array.

This means that there will be no crashes, as in C++

Please I do not mean to say that "C++ is bad" or similar nonsense.
The purpose of this post is to see if the C++ experts in this group
spot any errors in my reasoning. The C++ committee are bright people
and maybe I am missing something in the above examples.

jacob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top