digitalmars.D.learn - Cast a struct to void*
- John Colvin (11/11) Jan 09 2015 struct S
- anonymous (4/15) Jan 09 2015 You'd expect `cast(void*)s == s.p`? That doesn't work for any
- John Colvin (4/22) Jan 09 2015 I was expecting it to work regardless of the type of p. I have an
- Steven Schveighoffer (6/28) Jan 09 2015 This is actually a compiler bug! I will check to make sure it's not
- John Colvin (4/19) Jan 13 2015 So it is! The same happens with e.g. casting void* to string.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/21) Jan 09 2015 I know two options:
struct S { void* p; } void main() { S s; auto a = cast(void*)s; //Error: e2ir: cannot cast s of type S to type void* } Is there are a good reason for this being disallowed?
Jan 09 2015
On Friday, 9 January 2015 at 18:25:42 UTC, John Colvin wrote:struct S { void* p; } void main() { S s; auto a = cast(void*)s; //Error: e2ir: cannot cast s of type S to type void* } Is there are a good reason for this being disallowed?You'd expect `cast(void*)s == s.p`? That doesn't work for any type of p. You can do it with a slightly fancier (and more dangerous) cast: `*cast(void**)&s`.
Jan 09 2015
On Friday, 9 January 2015 at 18:35:56 UTC, anonymous wrote:On Friday, 9 January 2015 at 18:25:42 UTC, John Colvin wrote:I was expecting it to work regardless of the type of p. I have an 8 byte (on x86_64) struct which I want to reinterpret as a void*struct S { void* p; } void main() { S s; auto a = cast(void*)s; //Error: e2ir: cannot cast s of type S to type void* } Is there are a good reason for this being disallowed?You'd expect `cast(void*)s == s.p`? That doesn't work for any type of p.You can do it with a slightly fancier (and more dangerous) cast: `*cast(void**)&s`.Yuk. Better than nothing though. Thanks :)
Jan 09 2015
On 1/9/15 1:50 PM, John Colvin wrote:On Friday, 9 January 2015 at 18:35:56 UTC, anonymous wrote:This is actually a compiler bug! I will check to make sure it's not already filed, and file if it's not. However, I don't think the code should work, it just shouldn't print e2ir.On Friday, 9 January 2015 at 18:25:42 UTC, John Colvin wrote:struct S { void* p; } void main() { S s; auto a = cast(void*)s; //Error: e2ir: cannot cast s of type S to type void*This is what reinterpret_cast from C++ does. -SteveI was expecting it to work regardless of the type of p. I have an 8 byte (on x86_64) struct which I want to reinterpret as a void*} Is there are a good reason for this being disallowed?You'd expect `cast(void*)s == s.p`? That doesn't work for any type of p.You can do it with a slightly fancier (and more dangerous) cast: `*cast(void**)&s`.Yuk. Better than nothing though. Thanks :)
Jan 09 2015
On Friday, 9 January 2015 at 19:03:04 UTC, Steven Schveighoffer wrote:On 1/9/15 1:50 PM, John Colvin wrote:So it is! The same happens with e.g. casting void* to string. Annoyingly it passes __traits(compiles, ...)On Friday, 9 January 2015 at 18:35:56 UTC, anonymous wrote:This is actually a compiler bug!On Friday, 9 January 2015 at 18:25:42 UTC, John Colvin wrote:struct S { void* p; } void main() { S s; auto a = cast(void*)s; //Error: e2ir: cannot cast s of type S to type void*
Jan 13 2015
On 01/09/2015 10:25 AM, John Colvin wrote:struct S { void* p; } void main() { S s; auto a = cast(void*)s; //Error: e2ir: cannot cast s of type S to type void* } Is there are a good reason for this being disallowed?I know two options: a) alias p this; b) auto opCast(T : void*)() { return p; } Ali
Jan 09 2015