digitalmars.D - Taking the address of an rvalue struct
- dsimcha (17/17) Jul 22 2011 The following code uses the this reference in a struct member function t...
- Cristi Cobzarenco (27/44) Jul 22 2011 This is actually a question that has arisen in my GSoC problem, thanks
- Andrei Alexandrescu (6/23) Jul 22 2011 This has long been a feature that I attempted to remove several times.
- dsimcha (7/11) Jul 22 2011 I think invoking methods on rvalue structs needs to be allowed come Hell...
- Andrei Alexandrescu (6/17) Jul 22 2011 Prolonging the lifetime of temporaries to the end of the enclosing
- Cristi Cobzarenco (19/44) Jul 22 2011 I don't think it's neccessary to prolong their life till the end of
- Peter Alexander (6/13) Jul 22 2011 How would you stop it though without totally crippling the language?
- Andrei Alexandrescu (4/14) Jul 22 2011 That's why the feature hasn't been removed. Nevertheless, it's an
- dsimcha (17/25) Jul 22 2011 Well, it's slightly mitigated by only needing to prolong to the end of t...
The following code uses the this reference in a struct member function to take the address of an rvalue struct. Is it well-defined behavior equivalent to taking the address of a named stack-allocated struct, or is the compiler free to break it in optimized mode? struct SomeStruct { int num; this(int x) { num = x; } SomeStruct* addressOf() property { return &this; } } void main() { auto ptr = SomeStruct(42).addressOf; writeln(ptr.num); }
Jul 22 2011
This is actually a question that has arisen in my GSoC problem, thanks for asking David. In this case I'm pretty sure it would break if one allocated something else on the stack. The case I'm more interested in is something like this: struct Sum { Value *a, *b; int eval() { return *a + *b; } } struct Value { int v; Value* addressOf() property { return &this; } } void main() { writeln( Sum( Value(10).addressOf, Value(20).addressOf ).eval() ); } To me it seems that as long as all the literals are allocated in the same expression this should work, right? } --- Cristi Cobzarenco BSc in Artificial Intelligence and Computer Science University of Edinburgh Profile: http://www.google.com/profiles/cristi.cobzarenco On 22 July 2011 17:22, dsimcha <dsimcha yahoo.com> wrote:The following code uses the this reference in a struct member function to=takethe address of an rvalue struct. =A0Is it well-defined behavior equivalen=t totaking the address of a named stack-allocated struct, or is the compiler =freeto break it in optimized mode? struct SomeStruct { =A0 =A0int num; =A0 =A0this(int x) { =A0 =A0 =A0 num =3D x; =A0 =A0} =A0 =A0SomeStruct* addressOf() property { =A0 =A0 =A0 =A0return &this; =A0 =A0} } void main() { =A0 auto ptr =3D SomeStruct(42).addressOf; =A0 writeln(ptr.num); }
Jul 22 2011
On 7/22/11 10:22 AM, dsimcha wrote:The following code uses the this reference in a struct member function to take the address of an rvalue struct. Is it well-defined behavior equivalent to taking the address of a named stack-allocated struct, or is the compiler free to break it in optimized mode? struct SomeStruct { int num; this(int x) { num = x; } SomeStruct* addressOf() property { return&this; } } void main() { auto ptr = SomeStruct(42).addressOf; writeln(ptr.num); }This has long been a feature that I attempted to remove several times. It steps from the fact that you can invoke methods on rvalue structs. Disallowing that would solve the issue, but would also eliminate a lot of convenience. Andrei
Jul 22 2011
== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleThis has long been a feature that I attempted to remove several times.What? Taking the address of this? Invoking methods on rvalue structs?It steps from the fact that you can invoke methods on rvalue structs. Disallowing that would solve the issue, but would also eliminate a lot of convenience.I think invoking methods on rvalue structs needs to be allowed come Hell or high water. What's wrong with just banning any optimization of rvalue structs that have methods called on them, i.e. requiring the compiler to treat them the same as named methods, allocate stack space for them that doesn't get reused elsewhere in the same function, etc.?
Jul 22 2011
On 7/22/11 12:10 PM, dsimcha wrote:== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleThe latter.This has long been a feature that I attempted to remove several times.What? Taking the address of this? Invoking methods on rvalue structs?Prolonging the lifetime of temporaries to the end of the enclosing function would be interesting, but perhaps surprising. Not to mention inefficient. AndreiIt steps from the fact that you can invoke methods on rvalue structs. Disallowing that would solve the issue, but would also eliminate a lot of convenience.I think invoking methods on rvalue structs needs to be allowed come Hell or high water. What's wrong with just banning any optimization of rvalue structs that have methods called on them, i.e. requiring the compiler to treat them the same as named methods, allocate stack space for them that doesn't get reused elsewhere in the same function, etc.?
Jul 22 2011
I don't think it's neccessary to prolong their life till the end of the scope, but as it stands their lifetime has to be at least as long as the expression their in. I think that we should only disallow non-const method calls, const methods conceptually make sense on literals. Also disallowing method calls would conceivably also mean disallowing operator overloads to work and one would not be able to write stuff like Complex!double(-1.0,1.0) * z - which would be very inconvenient. --- Cristi Cobzarenco BSc in Artificial Intelligence and Computer Science University of Edinburgh Profile: http://www.google.com/profiles/cristi.cobzarenco On 22 July 2011 19:26, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:On 7/22/11 12:10 PM, dsimcha wrote:ts?=3D=3D Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleThis has long been a feature that I attempted to remove several times.What? =A0Taking the address of this? =A0Invoking methods on rvalue struc=The latter.ctsIt steps from the fact that you can invoke methods on rvalue structs. Disallowing that would solve the issue, but would also eliminate a lot of convenience.I think invoking methods on rvalue structs needs to be allowed come Hell or high water. =A0What's wrong with just banning any optimization of rvalue stru=hethat have methods called on them, i.e. requiring the compiler to treat them t=onsame as named methods, allocate stack space for them that doesn't get reused elsewhere in the same function, etc.?Prolonging the lifetime of temporaries to the end of the enclosing functi=would be interesting, but perhaps surprising. Not to mention inefficient. Andrei
Jul 22 2011
On 22/07/11 6:26 PM, Andrei Alexandrescu wrote:On 7/22/11 12:10 PM, dsimcha wrote:How would you stop it though without totally crippling the language? e.g. if I have a struct Vec3, I would expect to be able to do: Vec3 a = Vec3(1, 0, 0) + Vec3(0, 1, 0); How would you accomplish that without calling Vec3.opBinary!"+" on an rvalue Vec3?== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleThe latter.This has long been a feature that I attempted to remove several times.What? Taking the address of this? Invoking methods on rvalue structs?
Jul 22 2011
On 7/22/11 1:30 PM, Peter Alexander wrote:On 22/07/11 6:26 PM, Andrei Alexandrescu wrote:That's why the feature hasn't been removed. Nevertheless, it's an awkward part of the language. AndreiOn 7/22/11 12:10 PM, dsimcha wrote:How would you stop it though without totally crippling the language?== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleThe latter.This has long been a feature that I attempted to remove several times.What? Taking the address of this? Invoking methods on rvalue structs?
Jul 22 2011
== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'sWell, it's slightly mitigated by only needing to prolong to the end of the enclosing scope. That said, I forgot about the extent to which destructors complicate things. I was thinking that the worst that happens by prolonging the lifetime is you waste a little extra stack space. If it weren't for the desire to avoid ridiculously complicated rules with exception after exception, I'd say that the lifetime should be prolonged iff there's no d'tor. That said, I still think that calling methods on rvalues needs to be preserved come Hell or high water. I'd rather have the weird corner cases happen when someone takes the address of a this reference (relatively uncommon) than when someone tries to, e.g. add two rvalue BigInts (common). For example: auto num = BigInt(2) + BigInt(3); The bottom line here is that there is no non-surprising behavior, at least that I can think of. Disallowing rvalue method calls will surprise a lot of ppl w.r.t. operator overloading. Prolonging lifetimes will surprise a lot of ppl w.r.t. when d'tors get called. The status quo will surprise a lot of ppl w.r.t. undefined behavior when they take the address of the this reference on an rvalue.I think invoking methods on rvalue structs needs to be allowed come Hell or high water. What's wrong with just banning any optimization of rvalue structs that have methods called on them, i.e. requiring the compiler to treat them the same as named methods, allocate stack space for them that doesn't get reused elsewhere in the same function, etc.?Prolonging the lifetime of temporaries to the end of the enclosing function would be interesting, but perhaps surprising. Not to mention inefficient.
Jul 22 2011