Slimelite BBS

Re: templates in header e

Back to EMULATE

From Chris Downs · 1996-04-07 19:10 · #73647 in EMULATE

In reply to templates in header e by 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."

 MB> Because containers are of no value unless they are smart.  The STL is
 MB> a good example of a system which hides huge amounts of complexity.

 But it still has to work within the contraints of the C++ language.
 Should we use containers for polymorphic objects?  If so, how do we
 prevent the derived part from getting sliced? What about
 containers for objects where random access iterators are required and
 the costs of copying the objects is large?  Your "in principle"
 doesn't hold for so many of the most common situations.

 MB> How
 MB> many programmers are there who could code a red-black tree?  For
 MB> sophisticated data structures underlying containers?  Some containers
 MB> also need to be able to move their data elements or do garbage
 MB> collection, all of which expose differences in these paradigms.
 
 Sounds like an excellent argument for keeping objects OUT of
 containers.

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

 What are the features of C++ pointers that aren't "type safe"?
 
 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?

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

 But these aren't "temporary objects" in STL.  The constructor is called
 and new objects are created.  Objects are destroyed.  Yes, the containers
 use references for the algorithms, but when containers are shuffled,
 constructors and destructors are called.  There are way
 too many situations where holding the objects themselves in the
 containers will bog down the system.
 
 EB> What optimizations?  An example would be helpful here --
 EB> it's a bit to oblique for me to use as it stands.

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

 Operations thru pointers have their well understood performance
 implications.  Contrusting and destructing objects also have their
 performance implications.  The "general principle" is to understand
 these mutually exclusive implications and decide based on the
 specifics.
---
 þ Blue Wave/QWK v2.12 þ

--- WILDMAIL!/WC v4.12 
 * Origin:   (1:100/4.0)