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

Dmitry Olshansky dmitry.olsh at gmail.com
Wed May 15 12:30:35 PDT 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 at yahoo.com <mailto:schveiguy at yahoo.com>> wrote:
>
>     On Wed, 15 May 2013 11:08:33 -0400, Artur Skawina
>     <art.08.09 at gmail.com <mailto: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
>
>


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list