May 15, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | > // writeln(AddressOf(B.init)); //waiting for rvalue ref, DIP39
Did I miss something? Since when is sure that the DIP is implemented? o.O
|
May 15, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace Attachments:
| On Wed, May 15, 2013 at 12:35 PM, Namespace <rswhite4@googlemail.com> wrote:
> // writeln(AddressOf(B.init)); //waiting for rvalue ref, DIP39
>>
> Did I miss something? Since when is sure that the DIP is implemented? o.O
>
just wishful thinking :) actually this should've been:
// writeln(AddressOf(B.init^)); //waiting for rvalue ref, DIP39
|
May 15, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky Attachments:
| > > > > Won't this work? > *cast(void**)&object > >> >> great, works! I've added it to my dtools repo: https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L19 |
May 15, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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:
>
>> // writeln(AddressOf(B.init)); //waiting for rvalue ref, DIP39
>>>
>> Did I miss something? Since when is sure that the DIP is implemented? o.O
>>
>
> just wishful thinking :) actually this should've been:
> // writeln(AddressOf(B.init^)); //waiting for rvalue ref, DIP39
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++. :)
|
May 16, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On Wednesday, 15 May 2013 at 19:30:41 UTC, Dmitry Olshansky wrote:
> Won't this work?
> *cast(void**)&object
Right question is "is this guaranteed to work by spec?".
|
May 16, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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:
>> Won't this work?
>> *cast(void**)&object
>
> Right question is "is this guaranteed to work by spec?".
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.
|
May 16, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin Attachments:
| 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:
>>
>>> Won't this work?
>>> *cast(void**)&object
>>>
>>
>> Right question is "is this guaranteed to work by spec?".
>>
>
> 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.
>
|
May 16, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | 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:
>> On Wednesday, 15 May 2013 at 19:30:41 UTC, Dmitry Olshansky wrote:
>>> Won't this work?
>>> *cast(void**)&object
>>
>> Right question is "is this guaranteed to work by spec?".
>
> 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.
Then spec needs to be updated or this is not correct solution.
|
May 16, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | Am 15.05.2013 17:08, schrieb Artur Skawina:
> 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
>
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.
|
May 16, 2013 Re: cast(size_t)&c != 0, but c is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | 16-May-2013 13:31, Maxim Fomin пишет: > 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: >>> Won't this work? >>> *cast(void**)&object >> >> Right question is "is this guaranteed to work by spec?". > > 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. 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 Olshansky |
Copyright © 1999-2021 by the D Language Foundation