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

后端 未结 5 2258
余生分开走
余生分开走 2021-02-15 12:09

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:27

    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.

提交回复
热议问题