Template docs

rm roel.mathys at gmail.com
Wed Oct 11 11:15:37 PDT 2006


Josh Stern wrote:
> On Wed, 11 Oct 2006 18:47:18 +0200, rm wrote:
> 
> 
>> in your 2 examples I see 1 big difference,
>> in the first example you templatize (is it a word?) part of the
>> declaration of the class.
>> in the second example the whole class is templatized.
> 
> I noticed that the original example in the docs compiled okay (with
> suitable foo nonsense filled in) and then played around with trying to use
> it in different ways.   Below is an example that is just like the one in
> the docs (except for identifier names).  It compiles and runs.   I'm
> basically just trying to understand if  the docs are just completely wrong
> or if there is some subtle point about limitation that I'm missing even if
> the description of it needs improving (see note below).
> 
> ******************************************
> 
> import std.stdio;
> 
> 
> class BadFoo {
> 
> 
>   template TBar(T)
>     {
> 	T xx;			// Error???
> 	int tfunc(T t) { return t.sizeof; }	// Error???
>     }
> 
> 
> 
>   this() { 
> 
>     d_int = TBar!(int).tfunc(5);
>     TBar!(int).xx = 7;
> 
>   }
> 
>   int val() { return TBar!(int).xx*d_int; }
> 
>   int  d_int;
> 
> 
> }
> 
> 
> class Norm {
> 
>   int x;
>   int y;
> }
> 
> void main() {
> 
> 
> 
>   BadFoo bf = new BadFoo;
>   
> 
>   writefln(bf.val());
>   writefln(BadFoo.sizeof);
>   writefln(Norm.sizeof);
> } 
> **********************************************************
> 
> Output of the above is "28\n4\n4\n"   I was surprised by the 4's.
> Are they both correct?   That seems like the size of the pointer 
> to the class rather than the class itself, or am I missing something?
> 
> 

yep, that's a pointer size,
try this:
  writefln(Norm.classinfo.init.sizeof);

bye,
rm



More information about the Digitalmars-d-learn mailing list