Sorry, that was wrong. This is right:<div><br></div><div><div>struct Variant</div><div>{</div><div>public:</div><div><span class="" style="white-space:pre">        </span>TypeInfo type() @property</div><div><span class="" style="white-space:pre">  </span>{</div>
<div><span class="" style="white-space:pre">            </span>return _type;</div><div><span class="" style="white-space:pre">      </span>}</div><div><span class="" style="white-space:pre">  </span></div><div><span class="" style="white-space:pre">   </span>ref Type_ as(Type_)() @property</div>
<div><span class="" style="white-space:pre">    </span>{</div><div><span class="" style="white-space:pre">          </span>if(typeid(Type_) != _type)</div><div><span class="" style="white-space:pre">                 </span>throw new Exception("The requested static type doesn't match the available dynamic type.");</div>
<div><span class="" style="white-space:pre">                    </span></div><div><span class="" style="white-space:pre">           </span>return _value!Type_;</div><div><span class="" style="white-space:pre">       </span>}</div><div><span class="" style="white-space:pre">  </span></div>
<div><span class="" style="white-space:pre">    </span>ref Variant opAssign(Type_)(Type_ value_)</div><div><span class="" style="white-space:pre">  </span>{</div><div><span class="" style="white-space:pre">          </span>_type = typeid(Type_);</div>
<div><span class="" style="white-space:pre">            </span>_value!Type_ = value_;</div><div><span class="" style="white-space:pre">             </span>return this;</div><div><span class="" style="white-space:pre">       </span>}</div><div><span class="" style="white-space:pre">  </span></div>
<div>private:</div><div><span class="" style="white-space:pre">     </span>TypeInfo _type;</div><div><br></div><div><span class="" style="white-space:pre">   </span>union</div><div><span class="" style="white-space:pre">      </span>{</div>
<div><span class="" style="white-space:pre">            </span>template _value(Type_)</div><div><span class="" style="white-space:pre">             </span>{</div><div><span class="" style="white-space:pre">                  </span>Type_ _value;</div><div><span class="" style="white-space:pre">              </span>}</div>
<div><span class="" style="white-space:pre">    </span>}<span class="" style="white-space:pre"> </span></div><div>}</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Dec 5, 2012 at 1:06 PM, Gor Gyolchanyan <span dir="ltr"><<a href="mailto:gor.f.gyolchanyan@gmail.com" target="_blank">gor.f.gyolchanyan@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">But why aren't they non-static members? If they were non-static members the structure's size would still be compile-time, computed by the sum of sizes of all template instances. the .sizeof would work perfectly well.<div>

This would be an incredibly useful feature. Among other use cases this would allow having a fully type-safe and very fast Variant type:</div><div><br></div><div><div>struct Variant</div><div>{</div><div>public:</div><div>

<span style="white-space:pre-wrap">     </span>TypeInfo type() @property</div><div><span style="white-space:pre-wrap">        </span>{</div><div><span style="white-space:pre-wrap">                </span>return _type;</div><div><span style="white-space:pre-wrap">    </span>}</div>

<div><span style="white-space:pre-wrap">  </span></div><div><span style="white-space:pre-wrap"> </span>ref Type_ as(Type_)() @property</div><div><span style="white-space:pre-wrap">  </span>{</div><div><span style="white-space:pre-wrap">                </span>if(typeid(Type_) != _type)</div>

<div><span style="white-space:pre-wrap">                  </span>throw new Exception("The requested static type doesn't match the available dynamic type.");</div><div><span style="white-space:pre-wrap">                        </span></div>
<div><span style="white-space:pre-wrap">          </span>return _value!Type_;</div><div><span style="white-space:pre-wrap">     </span>}</div><div><span style="white-space:pre-wrap">        </span></div><div>private:</div><div><span style="white-space:pre-wrap">  </span>TypeInfo _type;</div>

<div><br></div><div><span style="white-space:pre-wrap"> </span>union</div><div><span style="white-space:pre-wrap">    </span>{</div><div><span style="white-space:pre-wrap">                </span>template _value(Type_)</div><div>
<span style="white-space:pre-wrap">             </span>{</div><div><span style="white-space:pre-wrap">                        </span>Type_ _value;</div><div><span style="white-space:pre-wrap">            </span>}</div><div><span style="white-space:pre-wrap">        </span>}<span style="white-space:pre-wrap">       </span></div>

<div>}</div></div><div><br></div><div>Is there any good reason why this feature wouldn't be a good idea?</div><div class="gmail_extra"><div><div class="h5"><br><br><div class="gmail_quote">On Wed, Dec 5, 2012 at 12:51 PM, Simen Kjaeraas <span dir="ltr"><<a href="mailto:simen.kjaras@gmail.com" target="_blank">simen.kjaras@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>On 2012-39-05 09:12, Gor Gyolchanyan <<a href="mailto:gor.f.gyolchanyan@gmail.com" target="_blank">gor.f.gyolchanyan@gmail.com</a>> wrote:<br>


<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Consider this piece of code:<br>
<br>
struct Test<br>
{<br>
template member(Type)<br>
{<br>
Type member;<br>
}<br>
}<br>
<br>
unittest<br>
{<br>
Test test;<br>
test.member!int = 0;<br>
test.member!long = 0;<br>
test.member!short = 0;<br>
import std.stdio; writeln(test.sizeof);<br>
assert(test.sizeof == int.sizeof + long.sizeof + short.sizeof); // fails<br>
assert(test.sizeof == 1); // succeeds<br>
}<br>
<br>
I don't get why the structure's size remains unchanged even after<br>
instantiating 3 members inside it.<br>
How can I get the real size of the structure, including all its members<br>
(template or not)?<br>
</blockquote>
<br></div></div>
You have. Its real size is 1, and the template instantiations are not member<br>
fields.<br>
<br>
A template (not a templated function [actually, sorta, but that's a different<br>
discussion]) is basically static - if you try instead Test.member!int, you<br>
will see that this works perfectly.<br>
<br>
What we see here is another example of the confusion that can be caused by<br>
static names being accessible through an instance:<br>
<br>
struct Foo {<br>
    static int n;<br>
}<br>
<br>
Foo f;<br>
<br>
f.n = 4;<span><font color="#888888"><br>
<br>
-- <br>
Simen<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br>Bye,<br>Gor Gyolchanyan.<br>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br>
</div>