www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Where are the template members?

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d04088d23fc60c904d016e929
Content-Type: text/plain; charset=UTF-8

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)?

-- 
Bye,
Gor Gyolchanyan.

--f46d04088d23fc60c904d016e929
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div><span class=3D"" style=3D"white-space:pre">Consider this piece of code=
:</span></div><div><span class=3D"" style=3D"white-space:pre"><br></span></=
div><div><span class=3D"" style=3D"white-space:pre">	</span>struct Test</di=
v><div><span class=3D"" style=3D"white-space:pre">	</span>{</div>
<div><span class=3D"" style=3D"white-space:pre">		</span>template member(Ty=
pe)</div><div><span class=3D"" style=3D"white-space:pre">		</span>{</div><d=
iv><span class=3D"" style=3D"white-space:pre">			</span>Type member;</div><=
div><span class=3D"" style=3D"white-space:pre">		</span>}</div>
<div><span class=3D"" style=3D"white-space:pre">	</span>}</div><div><br></d=
iv><div><span class=3D"" style=3D"white-space:pre">	</span>unittest</div><d=
iv><span class=3D"" style=3D"white-space:pre">	</span>{</div><div><span cla=
ss=3D"" style=3D"white-space:pre">		</span>Test test;</div>
<div><span class=3D"" style=3D"white-space:pre">		</span>test.member!int =
=3D 0;</div><div><span class=3D"" style=3D"white-space:pre">		</span>test.m=
ember!long =3D 0;</div><div><span class=3D"" style=3D"white-space:pre">		</=
span>test.member!short =3D 0;</div>
<div><span class=3D"" style=3D"white-space:pre">		</span>import std.stdio; =
writeln(test.sizeof);</div><div><span class=3D"" style=3D"white-space:pre">=
		</span>assert(test.sizeof =3D=3D int.sizeof + long.sizeof + short.sizeof)=
; // fails</div>
<div><span class=3D"" style=3D"white-space:pre">		</span>assert(test.sizeof=
 =3D=3D 1); // succeeds</div><div><span class=3D"" style=3D"white-space:pre=
">	</span>}</div><div><br></div><div>I don&#39;t get why the structure&#39;=
s size remains unchanged even after instantiating 3 members inside it.</div=

s (template or not)?</div><div><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br=

--f46d04088d23fc60c904d016e929--
Dec 05 2012
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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
Dec 05 2012
prev sibling next sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Wednesday, 5 December 2012 at 08:39:12 UTC, Gor Gyolchanyan 
wrote:
 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)?

Isn't this an accepts invalid bug? I don't see how it would be possible to add instance variables to a class or struct using templates...
Dec 05 2012
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Dec 05 2012
prev sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Wednesday, 5 December 2012 at 08:39:12 UTC, Gor Gyolchanyan 
wrote:
 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)?

The compiler should yell at you ! what you try to do here make no sense :D
Dec 05 2012