Casting clarification
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Casting clarification

Hybrid View

  1. #1
    Join Date
    Feb 2005

    Casting clarification

    I've read that using reinterpret_cast<> should rarely (if ever) be used. So, what is the proper thing to do in a case like this...

    uint64_t u = ; // Some unsigned int64 calculation
    int64_t* i = &u;
    This, of course, generates an error.
    error: invalid conversion from 'uint64_t*' to type 'int64_t*'

    int64_t* i = static_cast<int64_t*> (&u);
    And this generates the following error.
    error: invalid static_cast from type 'uint64_t*' to type 'int64_t*'

    int64_t* i = reinterpret_cast<int64_t*> (&u);
    This works but is considered bad practice since reinterpret_cast is really analogous to the old 'c' casting style (i.e. (int64_t*) &u In other words, there are no CPU instructions generated, it is simply a compiler directive indicating the bits are to be interpreted as the new type.

    It appears the solution would be this.
    int64_t* i = static_cast<int64_t*> (static_cast<void*> (&u));
    And this is what I have done for quite some time when a situation like this arises. However, what is this really doing? Isn't this analogous to the reinterpret_cast (aside from the fact that compiler instructions are really being generated)? But anything can be cast to a void*, and then from a void* to any other type*. So I assume the underlying bits are not translated to and from the intermediate void* type. Is this true? If so, then it appears you are accomplishing nothing as the internal bits for the data type are not translated.

    Is this really the appropriate thing to do? If anyone can help enlighten me it would be greatly appreciated. Thanks.

  2. #2
    Join Date
    Jan 2006

    Re: Casting clarification

    Checking the standard concerning reinterpret_cast:
    Quote Originally Posted by C++11 Clause 5.2.10 Paragraph 7
    An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue v of type "pointer to T1" is converted to the type "pointer to cv T2", the result is static_cast<cv T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types and the alignment requirements of T2 are no stricter than those of T1, or if either type is void. Converting a prvalue of type "pointer to T1" to the type "pointer to T2" (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value. The result of any other such pointer conversion is unspecified.
    Therefore, in your example, static_cast<int64_t*>(static_cast<void*>(&u)) is equivalent to reinterpret_cast<int64_t*>(&u).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center