Where are the template members?

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Dec 5 09:45:17 PST 2012


12/5/2012 12:39 PM, Gor Gyolchanyan пишет:
> Consider this piece of code:
>
> struct Test
> {
> template member(Type)
> {
> Type member;
> }
> }
>
> unittest
> {
> Test test;
> test.member!int = 0;
> test.member!long = 0;
> test.member!short = 0;
> import std.stdio; writeln(test.sizeof);
> assert(test.sizeof == int.sizeof + long.sizeof + short.sizeof); // fails
> assert(test.sizeof == 1); // succeeds
> }
>
> I don't get why the structure's size remains unchanged even after
> instantiating 3 members inside it.
> How can I get the real size of the structure, including all its members
> (template or not)?
>

I do suspect that template part can only be global (TLS). Then being 
inside of struct is just a visibility thing.

Think of it this way:
how one can put template inside of a fixed-sized instance of Test if 
there can be arbitrary amount of instantiations (and generally unknown 
before compiling all modules that may use it).

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list