June 10, 2004 Re: To copy an auto class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | Sorry, I guess I didn't make myself clear. What I meant to suggest was that 'auto' is just a subset of 'refcount'.
If you had a 'refcount' variable that never got copied anywhere else, then it would work exactly like 'auto'; it would get deleted when it went out of scope.
On the other hand, using 'refcount' lets you copy the reference to other places (like passing it to a function, or even storing it someplace). It means that you can copy references around without the ugliness of the RefCountHandler class I posted above.
Russ
Arcane Jill wrote:
> In article <caabq3$1gpe$1@digitaldaemon.com>, Russ Lewis says...
>
>>Doesn't this just imply that 'auto' should instead be 'refcount'?
>
>
> No, I'm afraid it's not going to be that simple. The reason that you can't
> (currently) copy an auto class reference is because it's a very dangerous thing
> to do. If you're going to do it at all (if Walter lets us), you have to know
> exactly what you're doing. The compiler won't do reference counting for you. IF
> that's the appropriate thing to do with a given RAII resource (and it might not
> be), then you'll have to do it yourself, the hard way.
>
>
>
>>If the compiler implemented refcounting on specially marked variables, then this would be easy...
>
>
> If only. Alas, no compiler can be a mind-reader. :)
>
> For non-auto classes of course, we don't need reference counting anyway. The
> normal behavior of the garbage collected does the same thing only better.
>
> Jill
>
>
|
June 11, 2004 Re: To copy an auto class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | In article <caai2r$1qk6$1@digitaldaemon.com>, Arcane Jill says... > [...] >The reason that you can't >(currently) copy an auto class reference is because it's a very dangerous thing >to do. If you're going to do it at all (if Walter lets us), you have to know >exactly what you're doing. I was thinking of a way to pass an auto reference to a function. Let's suppose that exist a particular type of function parameter, the auto parameter. An auto parameter is particular in the fact that you can't copy the reference to it, just like normal auto references: class MyClass { .. } int f(auto Myclass z, int n) { .. // here you can't take a copy of z } void g() { auto Myclass x = ...; // auto reference Myclass y = ...; // normal reference .. f(x, 2); // it's guaranteed that nobody will take a copy of x .. f(y, 2); // this is good, don't mind if you can/cannot take a copy .. } // here x is destroyed The difference between a regular auto reference it's that the object it's supposed to be already existant _before_ the function call, and it's supposed to be disposed _after_ the function return, when it goes out of its declaration scope. This will permit a certain degree of auto reference copying, while keeping the RAII-consistence. I think that, at a theoretical level, the compiler could infer wheter a parameter is or isn't an auto-parameter based on the following rules: 1 - if the parameter is copied, then it's non-auto 2 - if the parameter id passed as a non-auto function parameter, then it's non-auto 3 - else: it's auto (RAII-safe) I don't know if it can be really done in any case, nor if it's easy to do. Ciao |
June 11, 2004 Re: To copy an auto class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roberto Mariottini | In article <cacmuo$1qr8$1@digitaldaemon.com>, Roberto Mariottini says... > >I was thinking of a way to pass an auto reference to a function. You can do that already (but don't tell Walter ;) ). > auto class A > { > uint n; > } > > void incA(A* a) > { > ++a.n; > } > > int main(char[][] args) > { > auto A a; > incA(&a); > return 0; > } Arcane Jill |
June 12, 2004 Re: To copy an auto class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | AFAIK, that's a well known technique. It's part of D's upholding of the "Spirit of C" (http://www.comeaucomputing.com/faqs/genfaq.html#betterCgeneral), specifically "Trust the programmer" and "Don't prevent the programmer from doing what needs to be done". Still, it was worth mentioning again... "Arcane Jill" <Arcane_member@pathlink.com> wrote in message news:cad2jv$2chs$1@digitaldaemon.com... > In article <cacmuo$1qr8$1@digitaldaemon.com>, Roberto Mariottini says... > > > >I was thinking of a way to pass an auto reference to a function. > > You can do that already (but don't tell Walter ;) ). > > > auto class A > > { > > uint n; > > } > > > > void incA(A* a) > > { > > ++a.n; > > } > > > > int main(char[][] args) > > { > > auto A a; > > incA(&a); > > return 0; > > } > > Arcane Jill > > |
Copyright © 1999-2021 by the D Language Foundation