Jump to page: 1 2
Thread overview
auto*
Jul 06, 2017
FoxyBrown
Jul 06, 2017
H. S. Teoh
Jul 06, 2017
FoxyBrown
Jul 06, 2017
H. S. Teoh
Jul 06, 2017
Ali Çehreli
Jul 07, 2017
Patrick Schluter
Jul 06, 2017
Meta
Jul 06, 2017
H. S. Teoh
Jul 06, 2017
Meta
Jul 06, 2017
H. S. Teoh
Jul 06, 2017
bauss
Jul 06, 2017
H. S. Teoh
Jul 07, 2017
bauss
Jul 07, 2017
Meta
Jul 07, 2017
jmh530
Jul 07, 2017
Meta
Jul 07, 2017
Patrick Schluter
July 06, 2017
Create an auto pointer, handy in some cases and fits in the language as a natural generalization.

July 06, 2017
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

-- 
Truth, Sir, is a cow which will give [skeptics] no more milk, and so they are gone to milk the bull. -- Sam. Johnson
July 06, 2017
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?

auto x = ...

auto* x = ...

auto* is the pointerized version of auto.


e.g.,

int x = ...

int* x = ...

typeof(x) y = ...
typeof(x)* y = ...


obviously the rhs must be congruent with the type.

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.


July 06, 2017
On Thu, Jul 06, 2017 at 08:24:24PM +0000, FoxyBrown via Digitalmars-d wrote: [...]
> auto x = ...
> 
> auto* x = ...
> 
> auto* is the pointerized version of auto.
> 
> 
> e.g.,
> 
> int x = ...
> 
> int* x = ...
> 
> typeof(x) y = ...
> typeof(x)* y = ...
> 
> 
> obviously the rhs must be congruent with the type.
> 
> 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.

Huh?  How is it even remotely valid to cast a single pointer to a double pointer implicitly?  If foo is a non-pointer object, `auto p = &foo;` already gives you a pointer.  If foo itself is also a pointer, `auto p = &foo;` will already give you a double pointer. There is no need to "specify the type in a verbose way" at all.

You can't get a double pointer out of a single pointer without saving the single pointer in an addressable variable, e.g.:

	auto p = &foo;	// takes address of foo (single pointer)
	auto pp = &p;	// takes address of p (double pointer)

It's invalid to take the address of an rvalue, because an rvalue has no location. I.e., `&(&foo)` is illegal because &foo is an rvalue. Before you can make a double pointer to it, you have to designate some location where it is to be stored, so that the double pointer can point to that location.


T

-- 
The two rules of success: 1. Don't tell everything you know. -- YHL
July 06, 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

July 06, 2017
On Thursday, 6 July 2017 at 18:10:57 UTC, FoxyBrown wrote:
> Create an auto pointer, handy in some cases and fits in the language as a natural generalization.

It's been suggested before (as well as more powerful generalization for slices and associative arrays), but Andrei vetoed it so it probably won't be added even if somebody created a formal proposal for it.
July 06, 2017
On Thu, Jul 06, 2017 at 09:42:22PM +0000, Meta via Digitalmars-d wrote:
> On Thursday, 6 July 2017 at 18:10:57 UTC, FoxyBrown wrote:
> > Create an auto pointer, handy in some cases and fits in the language as a natural generalization.
> 
> It's been suggested before (as well as more powerful generalization for slices and associative arrays), but Andrei vetoed it so it probably won't be added even if somebody created a formal proposal for it.

I'm curious, what exactly was proposed?  Because I have a hard time understanding what's intended from the OP's description.


T

-- 
Obviously, some things aren't very obvious.
July 06, 2017
On Thursday, 6 July 2017 at 21:58:45 UTC, H. S. Teoh wrote:
> On Thu, Jul 06, 2017 at 09:42:22PM +0000, Meta via Digitalmars-d wrote:
>> On Thursday, 6 July 2017 at 18:10:57 UTC, FoxyBrown wrote:
>> > Create an auto pointer, handy in some cases and fits in the language as a natural generalization.
>> 
>> It's been suggested before (as well as more powerful generalization for slices and associative arrays), but Andrei vetoed it so it probably won't be added even if somebody created a formal proposal for it.
>
> I'm curious, what exactly was proposed?  Because I have a hard time understanding what's intended from the OP's description.
>
>
> T

Partial type inference. `auto*` declares a point to a type that is inferred from inspecting the RHS's type. The previous proposal was for doing `auto[] a = [0, 1, 2]` (a's type is inferred as int[]).
July 06, 2017
On Thu, Jul 06, 2017 at 10:31:10PM +0000, Meta via Digitalmars-d wrote:
> On Thursday, 6 July 2017 at 21:58:45 UTC, H. S. Teoh wrote:
> > On Thu, Jul 06, 2017 at 09:42:22PM +0000, Meta via Digitalmars-d wrote:
> > > On Thursday, 6 July 2017 at 18:10:57 UTC, FoxyBrown wrote:
> > > > Create an auto pointer, handy in some cases and fits in the language as a natural generalization.
> > > 
> > > It's been suggested before (as well as more powerful generalization for slices and associative arrays), but Andrei vetoed it so it probably won't be added even if somebody created a formal proposal for it.
> > 
> > I'm curious, what exactly was proposed?  Because I have a hard time understanding what's intended from the OP's description.
> > 
> > 
> > T
> 
> Partial type inference. `auto*` declares a point to a type that is inferred from inspecting the RHS's type. The previous proposal was for doing `auto[] a = [0, 1, 2]` (a's type is inferred as int[]).

But doesn't `auto a = [0, 1, 2];` *already* infer typeof(a) as int[]? What does `auto[]` add over what `auto` already does?


T

-- 
"Real programmers can write assembly code in any language. :-)" -- Larry Wall
July 06, 2017
On Thursday, 6 July 2017 at 23:12:03 UTC, H. S. Teoh wrote:
> On Thu, Jul 06, 2017 at 10:31:10PM +0000, Meta via Digitalmars-d wrote:
>> On Thursday, 6 July 2017 at 21:58:45 UTC, H. S. Teoh wrote:
>> > On Thu, Jul 06, 2017 at 09:42:22PM +0000, Meta via Digitalmars-d wrote:
>> > > On Thursday, 6 July 2017 at 18:10:57 UTC, FoxyBrown wrote:
>> > > > Create an auto pointer, handy in some cases and fits in the language as a natural generalization.
>> > > 
>> > > It's been suggested before (as well as more powerful generalization for slices and associative arrays), but Andrei vetoed it so it probably won't be added even if somebody created a formal proposal for it.
>> > 
>> > I'm curious, what exactly was proposed?  Because I have a hard time understanding what's intended from the OP's description.
>> > 
>> > 
>> > T
>> 
>> Partial type inference. `auto*` declares a point to a type that is inferred from inspecting the RHS's type. The previous proposal was for doing `auto[] a = [0, 1, 2]` (a's type is inferred as int[]).
>
> But doesn't `auto a = [0, 1, 2];` *already* infer typeof(a) as int[]? What does `auto[]` add over what `auto` already does?
>
>
> T

It does, but I think it's more a thing of knowing what exactly the auto is.

Let's say you have.

auto a = foo();

You have no idea what auto actually is in that case, but

auto* a = foo();

You know auto is a pointer of whatever foo returns.
« First   ‹ Prev
1 2