template this parameters

Steven Schveighoffer schveiguy at yahoo.com
Tue Jan 25 07:58:18 PST 2011


On Tue, 25 Jan 2011 10:44:23 -0500, Trass3r <un at known.com> wrote:

> Why do they exist

It tells you the exact type of this at the call site.  For const struct  
functions, this will tell you what the actual constness of the variable  
is.  For classes, this may give you the derived or base class or interface  
so you can do more if you want.  For example, you could return the derived  
type for chaining.

> and why does typeof(this) strip constness?

That seems like a bug, but actually, I'd not trust typeid.  typeid is  
actually a runtime-defined TypeInfo class, which you are calling toString  
on, which is not telling you exactly what the compiler thinks is the  
type.  Try this instead:

typeof(this).stringof

which is the type the compiler uses.  This might still return the wrong  
type.  I seem to remember typeof(this) is handled specially, so you can  
use it inside static functions.  You might try this:

auto x = this;
writeln(typeof(x).stringof);

I would still say it's a bug, it's very surprising that inside a const  
member function, typeof(this) does not assume the const attribute.

-Steve


More information about the Digitalmars-d-learn mailing list