Slimelite BBS

templates in header e

Back to EMULATE

From Mike Bilow · 1996-04-05 15:31 · #73602 in EMULATE

Ed Beroset wrote in a message to Mike Bilow:

 MB> In principle, a stack container should contain objects, not 
 MB> pointers to objects.  

 EB> Well, that's one way to do it, certainly.  You haven't really 
 EB> given much justification or explanation for why they "should."

Because containers are of no value unless they are smart.  The STL is a good example of a system which hides huge amounts of complexity.  How many programmers are there who could code a red-black tree?  For sophisticated data structures underlying containers?  Some containers also need to be able to move their data elements or do garbage collection, all of which expose differences in these paradigms.

 MB> While a reference is a pointer at a very 
 MB> low level, of course, they are conceptually different.  

 EB> Yes, they are.  I'm sure you have a point here, but I'm
 EB> failing to see it. 

Among other things, pointers are inherently not type-safe.  They can be made partially type-safe, but never completely so.

 MB> This conceptual distinction becomes important when defining operators, 
 MB> especially those which operate within the container, such as iterators.

 EB> Can you be specific?

The compiler is allowed to make "trivial" conversions between an object and a reference to a const object, for example.  This can lead to huge speed improvements by avoiding the copy of object, especially if they are large object, which would otherwise ensue.

 MB> In addition, there would be significant optimizations possible 
 MB> in the compiler if the function was defined to take a const 
 MB> reference.

 EB> What optimizations?  An example would be helpful here --
 EB> it's a bit to oblique for me to use as it stands.

Let's say that I define a class which ends up being implement by the compiler, at a low level, as an array.  Everyone knows that you want to avoid pushing the array onto and popping it off of the stack, or very time consuming function calls will result.  By allowing the compiler to make its trivial conversions, which are only allowed for references and not pointers, you can avoid all of this nasty function call overhead.
 
-- Mike


--- 
 * Origin: N1BEE BBS +1 401 944 8498 V.34/V.FC/V.32bis/HST16.8 (1:323/107)