Jump to page: 1 2
Thread overview
cast(size_t)&c != 0, but c is null
May 14, 2013
simendsjo
May 14, 2013
Andrej Mitrovic
May 14, 2013
simendsjo
May 14, 2013
Dicebot
May 15, 2013
David
May 15, 2013
Dicebot
May 15, 2013
Artur Skawina
May 15, 2013
Timothee Cour
May 15, 2013
Dmitry Olshansky
May 15, 2013
Timothee Cour
May 16, 2013
Dicebot
May 16, 2013
Maxim Fomin
May 16, 2013
Timothee Cour
May 16, 2013
Dicebot
May 16, 2013
Dmitry Olshansky
May 15, 2013
Namespace
May 15, 2013
Timothee Cour
May 15, 2013
Namespace
May 16, 2013
David
May 14, 2013
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: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
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:
>> 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

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...
May 15, 2013
Am 14.05.2013 14:24, schrieb Dicebot:
> 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.

A simple cast to void* should do it: cast(void*)c
May 15, 2013
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

Is it actually defined somewhere in spec?
May 15, 2013
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.

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
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:
>> 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
May 15, 2013
>> 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 '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:

> 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:
>>
>>> 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
>


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**)&object
>
> importstd.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
« First   ‹ Prev
1 2