www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert const(GUID)* to GUID*

reply Loopback <elliott.darfink gmail.com> writes:
Hello!

I've recently been working with DirectInput using the win32 API Binding
at dsource.org. This wrapper has been working perfectly fine until I
started using GUID constants defined in win32.directx.dinput8.

The directx function DirectInput8Create takes these parameters:
HRESULT DirectInput8Create(HINSTANCE, DWORD, GUID*, void**, IUnknown);

For the GUID* (3rd) parameter, I specify the IID_IDirectInput8 constant.
This is defined in the dinput8 file as follows:
const GUID IID_IDirectInput8A = {...};

If I supply this constant to the DirectInput8Create function the
compiler complains saying that it cannot convert const(GUID) to GUID*.
If I instead use &IID_IDirectInput8 (as a reference) I get the
"Cannot convert const(GUID)* to GUID*" instead. So my question is; how
do I solve this?
Jun 25 2011
next sibling parent Jimmy Cao <jcao219 gmail.com> writes:
--0016e6465190c10a9e04a68ed76e
Content-Type: text/plain; charset=ISO-8859-1

On Sat, Jun 25, 2011 at 2:48 PM, Loopback <elliott.darfink gmail.com> wrote:

 Hello!

 I've recently been working with DirectInput using the win32 API Binding
 at dsource.org. This wrapper has been working perfectly fine until I
 started using GUID constants defined in win32.directx.dinput8.

 The directx function DirectInput8Create takes these parameters:
 HRESULT DirectInput8Create(HINSTANCE, DWORD, GUID*, void**, IUnknown);

 For the GUID* (3rd) parameter, I specify the IID_IDirectInput8 constant.
 This is defined in the dinput8 file as follows:
 const GUID IID_IDirectInput8A = {...};

 If I supply this constant to the DirectInput8Create function the
 compiler complains saying that it cannot convert const(GUID) to GUID*.
 If I instead use &IID_IDirectInput8 (as a reference) I get the
 "Cannot convert const(GUID)* to GUID*" instead. So my question is; how
 do I solve this?

I think you can cast the const away. cast(GUID*)&IID_IDirectInput8 --0016e6465190c10a9e04a68ed76e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sat, Jun 25, 2011 at 2:48 PM, Loopbac= k <span dir=3D"ltr">&lt;<a href=3D"mailto:elliott.darfink gmail.com">elliot= t.darfink gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quo= te" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;= "> Hello!<br> <br> I&#39;ve recently been working with DirectInput using the win32 API Binding= <br> at <a href=3D"http://dsource.org" target=3D"_blank">dsource.org</a>. This w= rapper has been working perfectly fine until I<br> started using GUID constants defined in win32.directx.dinput8.<br> <br> The directx function DirectInput8Create takes these parameters:<br> HRESULT DirectInput8Create(HINSTANCE, DWORD, GUID*, void**, IUnknown);<br> <br> For the GUID* (3rd) parameter, I specify the IID_IDirectInput8 constant.<br=

const GUID IID_IDirectInput8A =3D {...};<br> <br> If I supply this constant to the DirectInput8Create function the<br> compiler complains saying that it cannot convert const(GUID) to GUID*.<br> If I instead use &amp;IID_IDirectInput8 (as a reference) I get the<br> &quot;Cannot convert const(GUID)* to GUID*&quot; instead. So my question is= ; how<br> do I solve this?<br> </blockquote></div><br><div><br></div><div><br></div><div>I think you can c= ast the const away.<div><div>cast(GUID*)<span class=3D"Apple-style-span" st= yle=3D"font-family: arial, sans-serif; font-size: 13px; background-color: r= gb(255, 255, 255); ">&amp;IID_IDirectInput8</span></div> </div></div> --0016e6465190c10a9e04a68ed76e--
Jun 25 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I've had this pop up in the past, I think you can safely do a cast
like Jimmy said.
Jun 25 2011
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-06-25 13:05, Jimmy Cao wrote:
 On Sat, Jun 25, 2011 at 2:48 PM, Loopback <elliott.darfink gmail.com> wrote:
 Hello!
 
 I've recently been working with DirectInput using the win32 API Binding
 at dsource.org. This wrapper has been working perfectly fine until I
 started using GUID constants defined in win32.directx.dinput8.
 
 The directx function DirectInput8Create takes these parameters:
 HRESULT DirectInput8Create(HINSTANCE, DWORD, GUID*, void**, IUnknown);
 
 For the GUID* (3rd) parameter, I specify the IID_IDirectInput8 constant.
 This is defined in the dinput8 file as follows:
 const GUID IID_IDirectInput8A = {...};
 
 If I supply this constant to the DirectInput8Create function the
 compiler complains saying that it cannot convert const(GUID) to GUID*.
 If I instead use &IID_IDirectInput8 (as a reference) I get the
 "Cannot convert const(GUID)* to GUID*" instead. So my question is; how
 do I solve this?

I think you can cast the const away. cast(GUID*)&IID_IDirectInput8

Well, you _can_, but casting away const is not technically defined in D. It should work at this point, but it's quite legal for a D compiler to _not_ work with casting away const. You're really not supposed to be casting away const. The best way to handle this is to have a GUID* which isn't const in the first place. Barring that, if the function that you're trying to pass it to is a C function, and you _know_ that that function isn't going to alter it, then you could always use a declaration for that function which makes the parameter const. If it's a D function and you know that it won't alter the variable, then it would probably be better to write a version of it which takes a const. Now, if you can't do either of those for some reason, and you _know_ that the function isn't going to alter the variable, then you can currently cast away const, and odds are that that will always work with any D compiler, but there's no guarantee that it will, because casting away const is undefined. Now, if the function actually _does_ alter the variable, then you need to get a mutable variable to pass to it. You'd just be asking for trouble to give a const variable to a function which will alter that variable, no matter how you manage to get the function take take it. - Jonathan M Davis
Jun 25 2011
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Jonathan M Davis Wrote:

 On 2011-06-25 13:05, Jimmy Cao wrote:
 I think you can cast the const away.
 cast(GUID*)&IID_IDirectInput8

Well, you _can_, but casting away const is not technically defined in D. It should work at this point, but it's quite legal for a D compiler to _not_ work with casting away const. You're really not supposed to be casting away const.

No, it is defined behavior: http://digitalmars.com/d/2.0/const3.html "The immutable type can be removed with a cast" "This does not mean, however, that one can change the data" "The ability to cast away immutable-correctness is necessary in some cases where the static typing is incorrect and not fixable, such as when referencing code in a library one cannot change. Casting is, as always, a blunt and effective instrument, and when using it to cast away immutable-correctness, one must assume the responsibility to ensure the immutableness of the data, as the compiler will no longer be able to statically do so." This is specific to immutable, but still applies to const. Casting away const/immutable is defined, modifying after the cast is not.
Jun 25 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-06-25 14:41, Jesse Phillips wrote:
 Jonathan M Davis Wrote:
 On 2011-06-25 13:05, Jimmy Cao wrote:
 I think you can cast the const away.
 cast(GUID*)&IID_IDirectInput8

Well, you _can_, but casting away const is not technically defined in D. It should work at this point, but it's quite legal for a D compiler to _not_ work with casting away const. You're really not supposed to be casting away const.

No, it is defined behavior: http://digitalmars.com/d/2.0/const3.html "The immutable type can be removed with a cast" "This does not mean, however, that one can change the data" "The ability to cast away immutable-correctness is necessary in some cases where the static typing is incorrect and not fixable, such as when referencing code in a library one cannot change. Casting is, as always, a blunt and effective instrument, and when using it to cast away immutable-correctness, one must assume the responsibility to ensure the immutableness of the data, as the compiler will no longer be able to statically do so." This is specific to immutable, but still applies to const. Casting away const/immutable is defined, modifying after the cast is not.

Ah yes. You're right I misremembered. Casting away const is defined and legal, but _modifying_ such a variable is not (unlike C++). - Jonathan M Davis
Jun 25 2011