Does D provide automatic dereferencing for accessing members through pointers?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 28 22:33:10 PDT 2014


On Fri, Aug 29, 2014 at 05:28:12AM +0000, Andrew Godfrey via Digitalmars-d-learn wrote:
> On Friday, 29 August 2014 at 05:05:55 UTC, H. S. Teoh via
> Digitalmars-d-learn wrote:
> >On Fri, Aug 29, 2014 at 04:37:37AM +0000, Andrew Godfrey via
> >Digitalmars-d-learn wrote:
> >>Unless the property you're accessing is also a pointer property,
> >>like sizeof. Then you have to be careful.
> >
> >True. Though if you're writing generic code, chances are that what
> >you want is the pointer size rather than the size of the referenced
> >object.  You only really get into trouble when you have to explicitly
> >work with pointers.
> 
> Thanks for the link.
> 'sizeof' is not so bad anyway because it's a property of the type.
> It would be worse if pointers had properties.
[...]

Which *could* happen if you use alias this, which is a common tool for
implementing transparent (or, in this case, not-so-transparent) type
wrappers:

	struct SmartPtr(T)
	{
		T* _impl;
		alias _impl this;

		@property dumbProperty() { return 1; }
	}

	struct MyObj
	{
		int dumbProperty = 2;
	}

	void func(T)(T t)
	{
		assert(t.dumbProperty == 2); // will fail
	}

	func(SmartPtr!MyObj.init); // oops


T

-- 
BREAKFAST.COM halted...Cereal Port Not Responding. -- YHL


More information about the Digitalmars-d-learn mailing list