Safer casts

Yigal Chripun yigal100 at gmail.com
Sun May 11 17:10:49 PDT 2008


Janice Caron wrote:
> On 11/05/2008, Yigal Chripun <yigal100 at gmail.com> wrote:
>> From what I know, an efficient STL implementation would use C style
>>  containers with void* under the hood to minimize executable bloat.
> 
> I /think/ I get what you're saying here, which is (correct me if I'm
> wrong, but) given
> 
>     class C {}
>     class D {}
> 
> then
> 
>     Vector!(C) c;
>     Vector!(D) d;
> 
> produces two instantiations of Vector! - one for C and one for D - and
> yet those instantiations will produce byte-for-byte identical machine
> code.
> 
> However - this is a linker problem, not a template problem, and Walter
> has said that this will be fixed in the future. So in the future,
> whenever any two or more segments consist of identical bytes, the
> linker may keep one and throw the rest away.
> 
> The moral of the story is: specify your intent, but leave the
> implementation to the compiler/linker.
> 
> If you try to "outguess" the compiler, by instantiating only one
> single Vector!(void*), then in the long run, all you are doing is
> creating obfuscated code for short term gain.

I don't know about any plans to change the dm linker. it is currently
written directly in assembly and highly optimized. any change would
require a lot of work and probably a rewrite. also, if walter ever
decides to work on the linker he should change the format of the object
files. Personally i don't understand a great deal about that subject,
but i got the impression in this NG from more knowledgeable people that
such a change is needed.
what i mean was doing something like this:
for example for list - defining a regular LinkedListImpl class which
holds void* as items, and then defining a class LinkedList(T) which uses
internally the LinkedListImpl class. this way the templates and
interface hierarchy is separate from the implementation.
the templatized interfaces and classes in the hierarchy provide the high
level API and the *Impl classes provide efficient implementations
without bloating the executable code. you get a different instances of
vector as per your example for each unique type T, BUT those
instantiations are merely small shells that call common code in the
matching vectorImpl class.
that is not the same as using linkedList!(void*) since you do get a
layer that provides the convenience and type safety. If the
linker/compiler could automate this, that would be great. but it can be
accomplished without that too.



More information about the Digitalmars-d mailing list