Where are the template members?
Ali Çehreli
acehreli at yahoo.com
Wed Dec 5 14:44:14 PST 2012
On 12/05/2012 12:39 AM, Gor Gyolchanyan wrote:
> Consider this piece of code:
>
> struct Test
> {
> template member(Type)
> {
> Type member;
> }
That is just a template definition. It is not instantiated in this
struct for any type.
> }
>
> unittest
> {
> Test test;
That is an object of an empty struct.
> test.member!int = 0;
> test.member!long = 0;
> test.member!short = 0;
Those lines use a template that is defined inside the struct. They
instantiate the template for three types. I don't know how the 'member'
member of the template gets used in this context (it looks like the bug
that Rene Zwanenburg mentions) but I know that it still has no effect on
the Test type.
> 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)?
It does not have any members. One way of injecting members would be to
use template mixins or string mixins.
Ali
More information about the Digitalmars-d
mailing list