toString() on struct template

Daniel Keep daniel.keep.lists at gmail.com
Thu Mar 1 02:20:38 PST 2007



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.

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.

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list