auto*

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 6 13:58:04 PDT 2017


On 07/06/2017 01:24 PM, FoxyBrown wrote:
 > On Thursday, 6 July 2017 at 18:11:13 UTC, H. S. Teoh wrote:
 >> On Thu, Jul 06, 2017 at 06:10:57PM +0000, FoxyBrown via Digitalmars-d
 >> wrote:
 >>> Create an auto pointer, handy in some cases and fits in the language
 >>> as a natural generalization.
 >>
 >> What's an auto pointer?
 >>
 >>
 >> T
 >
 > is it not obvious?

It wasn't obvious at all. :) (I even suspected C++'s std::auto_ptr but 
dropped the thought.)

 > auto p = &foo;  // a pointer
 > auto* p = &foo; // a double pointer to foo.
 >
 > When having the need to require a pointer to a pointer, it avoids having
 > to specify the type in a verbose way.
 >
 > i.e., the second line above is not valid, but to do it we must either
 > cast to void** or use typeof and such to get the correct type and make a
 > pointer out of it. auto* simplifies all that.

Let's say foo is type Foo...

Staying with that example and assuming that p has type Foo**, what would 
*p provide? We have the object (foo), we have the pointer to pointer 
(p), but where does the pointer itself live? Only the programmer knows 
where that intermediate pointer is. For example:

struct Foo {
}

void main() {
     auto foo = Foo();
     auto x = &foo;    // A local variable here
     auto p = &x;
}

It seems to work cleanly to me. If you have something else in mind 
please show the troubling line with complete code. :)

Ali



More information about the Digitalmars-d mailing list