www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to tell how an object/class is declared

reply "Meta" <jared771 gmail.com> writes:
Say I have the following struct and object definitions:

struct Test1(T)
{
     static if (???)
     {
         void doSomething()
         {
             writeln(typeof(this).stringof);
         }
     }

     T t;
}

class Test2(T)
{
     static if (???)
     {
         void doSomething()
         {
             writeln(typeof(this).stringof);
         }
     }

     T t;
}

const(Test1!int) t1i;
shared(Test2!int) t2s;

How can I tell at the points marked in the above code if the 
class or struct instance was declared as const, shared, etc.? Is 
this possible?
Nov 06 2014
next sibling parent "Meta" <jared771 gmail.com> writes:
One other thing. I know about `template this`, but I'm not sure 
if that's a tenable solution or not in my case. In addition to 
templating the struct or class, would I not also have to template 
the constructor so it could pick up the type of `this` at the 
declaration site?
Nov 06 2014
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 6 November 2014 at 23:43:19 UTC, Meta wrote:
 How can I tell at the points marked in the above code if the 
 class or struct instance was declared as const, shared, etc.? 
 Is this possible?
You can't do that, but you can overload on const void doSomething() const { called on const instance } void doSomething() { called on mutable }
Nov 06 2014