Disabling “cast from pointer to smaller type uint32_t” error in Clang

后端 未结 5 2314
灰色年华
灰色年华 2021-02-15 12:07

I\'m working on a school project that involves porting a large piece of C++ code on an experimental piece of hardware. Unfortunately, that hardware is 64-bit and the code contai

5条回答
  •  再見小時候
    2021-02-15 12:43

    Save this piece of code as mycast.hpp and add -include mycast.hpp to your Makefile.

    #include 
    
    template
    U Reinterpret_cast(T *x) {
        return (U)(uintptr_t)x;
    }
    
    template
    U Reinterpret_cast(T &x) {
        return *(U*)&x;
    }
    
    #define reinterpret_cast Reinterpret_cast
    

    They should do their job unless your code is too tricky.

    Your strategy will not work for stack-allocated objects, be careful!! You can insert some debugging/logging logic to Reinterpret_cast if necessary.

提交回复
热议问题