digitalmars.D.learn - cast(size_t)&c != 0, but c is null
- simendsjo (8/8) May 14 2013 Very newbie question coming up :)
- Andrej Mitrovic (4/12) May 14 2013 That's the address of the reference. If you want the address of
- simendsjo (4/18) May 14 2013 Ah. Thanks to you both. Now I have some strange bug where
- Dicebot (4/12) May 14 2013 "&c" is address of reference, no class instance. I don't know if
- David (2/16) May 15 2013 A simple cast to void* should do it: cast(void*)c
- Dicebot (2/7) May 15 2013 Is it actually defined somewhere in spec?
- Artur Skawina (9/14) May 15 2013 class C { auto opCast(T:void*)() { return null; } }
- Steven Schveighoffer (4/14) May 15 2013 You can insert a cast to Object first.
- Timothee Cour (35/57) May 15 2013 doesn't seem to work.
- Dmitry Olshansky (5/53) May 15 2013 Won't this work?
- Timothee Cour (3/7) May 15 2013 great, works!
- Dicebot (2/4) May 16 2013 Right question is "is this guaranteed to work by spec?".
- Maxim Fomin (4/9) May 16 2013 Spec is silent about this but in practice this should work. Real
- Timothee Cour (2/14) May 16 2013
- Dicebot (2/13) May 16 2013 Then spec needs to be updated or this is not correct solution.
- Dmitry Olshansky (8/17) May 16 2013 Since class is a reference it's a pointer value (address anyhow).
- Namespace (2/4) May 15 2013 Did I miss something? Since when is sure that the DIP is
- Timothee Cour (3/6) May 15 2013 just wishful thinking :) actually this should've been:
- Namespace (4/15) May 15 2013 I know that feeling. :D But I doubt that D will have such a thing
- David (9/29) May 16 2013 Overloading cast is broken and shouldn't be in the language (imo). Why
Very newbie question coming up :) How does D mark null values for classes? `c is null` returns true, but `&c` isn't 0. So how does D know `c is null`? class C {} C c; assert(c is null); assert(cast(size_t)&c == 0); // fails.
May 14 2013
On Tuesday, 14 May 2013 at 12:18:20 UTC, simendsjo wrote:Very newbie question coming up :) How does D mark null values for classes? `c is null` returns true, but `&c` isn't 0. So how does D know `c is null`? class C {} C c; assert(c is null); assert(cast(size_t)&c == 0); // fails.That's the address of the reference. If you want the address of the object: assert(cast(size_t)cast(void*)c == 0); // passes
May 14 2013
On Tuesday, 14 May 2013 at 12:20:00 UTC, Andrej Mitrovic wrote:On Tuesday, 14 May 2013 at 12:18:20 UTC, simendsjo wrote:Ah. Thanks to you both. Now I have some strange bug where somewhere in my code (probably using alias for passing symbols to templates), it get's a "wrong" reference...Very newbie question coming up :) How does D mark null values for classes? `c is null` returns true, but `&c` isn't 0. So how does D know `c is null`? class C {} C c; assert(c is null); assert(cast(size_t)&c == 0); // fails.That's the address of the reference. If you want the address of the object: assert(cast(size_t)cast(void*)c == 0); // passes
May 14 2013
On Tuesday, 14 May 2013 at 12:18:20 UTC, simendsjo wrote:Very newbie question coming up :) How does D mark null values for classes? `c is null` returns true, but `&c` isn't 0. So how does D know `c is null`? class C {} C c; assert(c is null); assert(cast(size_t)&c == 0); // fails."&c" is address of reference, no class instance. I don't know if there is a way to get a pointer to class instance in D but I am not aware of one.
May 14 2013
Am 14.05.2013 14:24, schrieb Dicebot:On Tuesday, 14 May 2013 at 12:18:20 UTC, simendsjo wrote:A simple cast to void* should do it: cast(void*)cVery newbie question coming up :) How does D mark null values for classes? `c is null` returns true, but `&c` isn't 0. So how does D know `c is null`? class C {} C c; assert(c is null); assert(cast(size_t)&c == 0); // fails."&c" is address of reference, no class instance. I don't know if there is a way to get a pointer to class instance in D but I am not aware of one.
May 15 2013
On Wednesday, 15 May 2013 at 10:31:29 UTC, David wrote:Is it actually defined somewhere in spec?"&c" is address of reference, no class instance. I don't know if there is a way to get a pointer to class instance in D but I am not aware of one.A simple cast to void* should do it: cast(void*)c
May 15 2013
On 05/15/13 13:04, Dicebot wrote:On Wednesday, 15 May 2013 at 10:31:29 UTC, David wrote:class C { auto opCast(T:void*)() { return null; } } So - no - a simple cast to void* won't always work, as the op can be overloaded, even if only by accident. See also: http://forum.dlang.org/thread/mutvhrrdfzunbrhmvztp forum.dlang.org#post-mailman.2432.1354406246.5162.digitalmars-d-learn:40puremagic.com and http://d.puremagic.com/issues/show_bug.cgi?id=8545 artur"&c" is address of reference, no class instance. I don't know if there is a way to get a pointer to class instance in D but I am not aware of one.A simple cast to void* should do it: cast(void*)c
May 15 2013
On Wed, 15 May 2013 11:08:33 -0400, Artur Skawina <art.08.09 gmail.com> wrote:On 05/15/13 13:04, Dicebot wrote:You can insert a cast to Object first. -SteveOn Wednesday, 15 May 2013 at 10:31:29 UTC, David wrote:class C { auto opCast(T:void*)() { return null; } } So - no - a simple cast to void* won't always work, as the op can be overloaded, even if only by accident."&c" is address of reference, no class instance. I don't know if there is a way to get a pointer to class instance in D but I am not aware of one.A simple cast to void* should do it: cast(void*)c
May 15 2013
doesn't seem to work. I'm trying to write a generic solution but having an innocent 'T opCast(T : int)() { return 1; }' breaks it, see Error below. doesn't seem to work. This seems to call for a compiler solution? import std.stdio; import std.conv; void*AddressOf(T)(T a) if(is(T==class)){ return cast(void*)cast(Object) a;//Error: template instance opCast!( Object) opCast!(Object) does not match template declaration opCast(T : int)( ) } T*AddressOf(T)(ref T a) if(!is(T==class)){ return &a; } class A{ int x; T opCast(T : int)() { return 1; } // int opCast(T:void*)() { return 1; } //won't work either } struct B{ } void main(){ A a; writeln(AddressOf(a)); a = new A; writeln(AddressOf(a)); B b; writeln(AddressOf(b)); B*b2; writeln(AddressOf(*b2)); // writeln(AddressOf(B.init)); //waiting for rvalue ref, DIP39 } On Wed, May 15, 2013 at 11:30 AM, Steven Schveighoffer <schveiguy yahoo.com>wrote:You can insert a cast to Object first.On Wed, 15 May 2013 11:08:33 -0400, Artur Skawina <art.08.09 gmail.com> wrote: On 05/15/13 13:04, Dicebot wrote:You can insert a cast to Object first. -SteveOn Wednesday, 15 May 2013 at 10:31:29 UTC, David wrote:class C { auto opCast(T:void*)() { return null; } } So - no - a simple cast to void* won't always work, as the op can be overloaded, even if only by accident."&c" is address of reference, no class instance. I don't know if thereis a way to get a pointer to class instance in D but I am not aware of one.A simple cast to void* should do it: cast(void*)c
May 15 2013
15-May-2013 23:17, Timothee Cour пишет:>> You can insert a cast to Object first. doesn't seem to work. I'm trying to write a generic solution but having an innocent 'TopCast(T:int)(){return1;}' breaks it, see Error below. doesn't seem to work. This seems to call for a compiler solution?Won't this work? *cast(void**)&objectimportstd.stdio; importstd.conv; void*AddressOf(T)(Ta)if(is(T==class)){ returncast(void*)cast(Object)a;//Error:templateinstanceopCast!(Object)opCast!(Object)doesnotmatchtemplatedeclarationopCast(T:int)() } T*AddressOf(T)(refTa)if(!is(T==class)){ return&a; } classA{ intx; TopCast(T:int)(){return1;} //intopCast(T:void*)(){return1;} //won't work either } structB{ } voidmain(){ Aa; writeln(AddressOf(a)); a=newA; writeln(AddressOf(a)); Bb; writeln(AddressOf(b)); B*b2; writeln(AddressOf(*b2)); //writeln(AddressOf(B.init));//waitingforrvalueref,DIP39 } On Wed, May 15, 2013 at 11:30 AM, Steven Schveighoffer <schveiguy yahoo.com <mailto:schveiguy yahoo.com>> wrote: On Wed, 15 May 2013 11:08:33 -0400, Artur Skawina <art.08.09 gmail.com <mailto:art.08.09 gmail.com>> wrote: On 05/15/13 13:04, Dicebot wrote: On Wednesday, 15 May 2013 at 10:31:29 UTC, David wrote: "&c" is address of reference, no class instance. I don't know if there is a way to get a pointer to class instance in D but I am not aware of one. A simple cast to void* should do it: cast(void*)c class C { auto opCast(T:void*)() { return null; } } So - no - a simple cast to void* won't always work, as the op can be overloaded, even if only by accident. You can insert a cast to Object first. -Steve-- Dmitry Olshansky
May 15 2013
Won't this work? *cast(void**)&objectgreat, works! I've added it to my dtools repo: https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L19
May 15 2013
On Wednesday, 15 May 2013 at 19:30:41 UTC, Dmitry Olshansky wrote:Won't this work? *cast(void**)&objectRight question is "is this guaranteed to work by spec?".
May 16 2013
On Thursday, 16 May 2013 at 09:05:46 UTC, Dicebot wrote:On Wednesday, 15 May 2013 at 19:30:41 UTC, Dmitry Olshansky wrote:Spec is silent about this but in practice this should work. Real question is here why would you want "pointer to class" when you have normal reference and what such "pointer" would give you.Won't this work? *cast(void**)&objectRight question is "is this guaranteed to work by spec?".
May 16 2013
interface with C++ for example (for example extern(C++) classes ) On Thu, May 16, 2013 at 2:31 AM, Maxim Fomin <maxim maxim-fomin.ru> wrote:On Thursday, 16 May 2013 at 09:05:46 UTC, Dicebot wrote:On Wednesday, 15 May 2013 at 19:30:41 UTC, Dmitry Olshansky wrote:Spec is silent about this but in practice this should work. Real question is here why would you want "pointer to class" when you have normal reference and what such "pointer" would give you.Won't this work? *cast(void**)&objectRight question is "is this guaranteed to work by spec?".
May 16 2013
On Thursday, 16 May 2013 at 09:31:11 UTC, Maxim Fomin wrote:On Thursday, 16 May 2013 at 09:05:46 UTC, Dicebot wrote:Then spec needs to be updated or this is not correct solution.On Wednesday, 15 May 2013 at 19:30:41 UTC, Dmitry Olshansky wrote:Spec is silent about this but in practice this should work. Real question is here why would you want "pointer to class" when you have normal reference and what such "pointer" would give you.Won't this work? *cast(void**)&objectRight question is "is this guaranteed to work by spec?".
May 16 2013
16-May-2013 13:31, Maxim Fomin пишет:On Thursday, 16 May 2013 at 09:05:46 UTC, Dicebot wrote:Since class is a reference it's a pointer value (address anyhow). Hence you'll get an address of value holding that address (e.g. of a temporary) then dereference it as void** pointer. It's sometimes useful in this particular case to circumvent type-system in order to do smth (interface with C etc.). -- Dmitry OlshanskyOn Wednesday, 15 May 2013 at 19:30:41 UTC, Dmitry Olshansky wrote:Spec is silent about this but in practice this should work. Real question is here why would you want "pointer to class" when you have normal reference and what such "pointer" would give you.Won't this work? *cast(void**)&objectRight question is "is this guaranteed to work by spec?".
May 16 2013
// writeln(AddressOf(B.init)); //waiting for rvalue ref, DIP39Did I miss something? Since when is sure that the DIP is implemented? o.O
May 15 2013
On Wed, May 15, 2013 at 12:35 PM, Namespace <rswhite4 googlemail.com> wrote:// writeln(AddressOf(B.init)); //waiting for rvalue ref, DIP39just wishful thinking :) actually this should've been: // writeln(AddressOf(B.init^)); //waiting for rvalue ref, DIP39Did I miss something? Since when is sure that the DIP is implemented? o.O
May 15 2013
On Wednesday, 15 May 2013 at 20:15:26 UTC, Timothee Cour wrote:On Wed, May 15, 2013 at 12:35 PM, Namespace <rswhite4 googlemail.com> wrote:I know that feeling. :D But I doubt that D will have such a thing some day. But: good luck, it is about time that D catch up fully to C++. :)// writeln(AddressOf(B.init)); //waiting for rvalue ref, DIP39just wishful thinking :) actually this should've been: // writeln(AddressOf(B.init^)); //waiting for rvalue ref, DIP39Did I miss something? Since when is sure that the DIP is implemented? o.O
May 15 2013
Am 15.05.2013 17:08, schrieb Artur Skawina:On 05/15/13 13:04, Dicebot wrote:Overloading cast is broken and shouldn't be in the language (imo). Why broken? If you only want to overload cast(bool) (for if(x) which calls if(cast(bool)x -> x.opCast!bool()) you have to overload all other possible casts, otherwise compiler will complain that this cast is no overloaded. Also overloading a cast doesn't really make any sense (except the bool case), that's also the reason why I really hate std.conv.to's new behaviour of calling opCast, this makes no sense, to! does a conversion not a cast.On Wednesday, 15 May 2013 at 10:31:29 UTC, David wrote:class C { auto opCast(T:void*)() { return null; } } So - no - a simple cast to void* won't always work, as the op can be overloaded, even if only by accident. See also: http://forum.dlang.org/thread/mutvhrrdfzunbrhmvztp forum.dlang.org#post-mailman.2432.1354406246.5162.digitalmars-d-learn:40puremagic.com and http://d.puremagic.com/issues/show_bug.cgi?id=8545 artur"&c" is address of reference, no class instance. I don't know if there is a way to get a pointer to class instance in D but I am not aware of one.A simple cast to void* should do it: cast(void*)c
May 16 2013