toString() on struct template

Henning Hasemann hhasemann at web.de
Thu Mar 1 09:46:04 PST 2007


On Thu, 01 Mar 2007 21:20:38 +1100
Daniel Keep <daniel.keep.lists at gmail.com> wrote:

> 
> 
> Henning Hasemann wrote:
> > I think I got it now:
> > If you build the source together with the main function, there
> > is no problem.
> > 
> > However in my constellation (the struct an the code that calls toString
> > in a linux static library which is linked against a main function)
> > this error occurs. The stacktrace says something about TypeInfo IIRC.
> > 
> > So it seems this information gets lost somehow?
> > I use dmd-1.007 (tried with 1.005 too), build the library with rebuild-0.12.
> > Maybe I should say that I use derelict too, but the error also occurs when
> > toString is called before any derelict-function.
> > 
> > I will try to further reduce the example, maybe later today.
> > 
> > Henning
> 
> You mentioned a library...  If I remember correctly (standard
> disclaimers apply), then templates are NOT compiled into a library.
> 
> For example, let's say you compiled the following file into a library:
> 
> struct Foo(T)
> {
>     void bar(T value)
>     {
>         writefln("%s", value);
>     }
> }
> 
> And then used it like so:
> 
> void main()
> {
>     Foo!(int) x;
>     x.bar(42);
> }
> 
> And finally compiled it like thus:
> 
>     dmd FooLib.lib main.d
> 
> This would fail since FooLib.lib doesn't actually *have* the
> implementation of Foo in it.  In order for D to compile a templated
> struct or class into a library, it would need to compile it for every
> possible type, which obviously isn't going to happen.

Sounds logical to me, but atm the only type I use for T is int
and the template is instantiated in the library so the version used should be
implemented.
Calling other methods seems to work, too.

> If you want to use templates in a library, you *need* to ship and
> compile against the source code, not against a precompiled library.  The
> one exception to this is that it should work for any templates you've
> explicitly instantiated. So, if you append this to Foo's source file:
> 
>   alias Foo!(int) FooInt;
> 
> then it should work.

And that's what I'm doing, too. So no problem here. Ill now start to find a
minimal example of the problem.

Henning



More information about the Digitalmars-d mailing list