cast(size_t)&c != 0, but c is null

Timothee Cour thelastmammoth at gmail.com
Wed May 15 12:17:35 PDT 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 at yahoo.com>wrote:

> On Wed, 15 May 2013 11:08:33 -0400, Artur Skawina <art.08.09 at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130515/b113adb9/attachment.html>


More information about the Digitalmars-d-learn mailing list