Article View: comp.std.c++
Article #2262Re: Determining between classes and built-in t
From: Scott Howlett
Date: Thu, 19 Oct 1995 21:31
Date: Thu, 19 Oct 1995 21:31
47 lines
2736 bytes
2736 bytes
Distribution: In article <...>, kanze@gabi-soft.fr (J. Kanze) wrote: > Cay S. Horstmann (horstman@jupiter.SJSU.EDU) wrote: > > |> I did have this problem [determining whether a type is built-in or > |> not] when trying to write a decent array template. > |> For numeric and pointer types, I'd like to use memmove when the array > |> grows and is relocated. For class types that cannot be done in > |> general. I did "solve" the problem by having two templates, but now > |> the risk is that someone who doesn't understand the distinction uses > |> the "BuiltinTypeArray" (not its real name) with a class type, which > |> might have bizarre results. So I think Brett's desire is not totally > |> outlandish. > > At least for your problem, I would just write the loop and forget it. > In my experience it has been beneficial to distinguish not between types which can be shuffled around via memmove and those which cannot, but between types which should be created with default constructors and those which should only be created in the array with placement new. For types which have constructors and destructors, it's usually quicker to manage the storage for the array as an array of characters, with elements managed by placement new and explicit destruction. For types which do not have constructors and destructors, it's quicker to manage the array storage directly as an array of objects of that type. In both cases, I've found that assignment via a loop was quicker than assignment via memmove. The clever part is to construct the array templates so that for any type you can just write MyArray<T> and based on info you have elsewhere provided about T, an appropriate implementation is chosen at compile time. Through a couple of iterations of my implementation, I've also managed to factor out ~80% of the implementation of the array into a type-independent base class. -- Scott Howlett Storm Software, Inc. howlett@netcom.com --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<howlett-1910951419080001@192.0.2.1>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU> <46594d$5b9@gabi.gabi-soft.fr>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: howlett@netcom.com (Scott Howlett) Newsgroups: comp.std.c++ Subject: Re: Determining between classes and built-in t Date: 19 Oct 1995 21:31:50 GMT Organization: Storm Software, Inc. Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <howlett-1910951419080001@192.0.2.1> References: <Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU> <46594d$5b9@gabi.gabi-soft.fr> NNTP-Posting-Host: physics2.berkeley.edu