December 05, 2012
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.


December 05, 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
December 05, 2012
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...
December 05, 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

December 05, 2012
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
Top | Discussion index | About this forum | D home