C->C++ Automatically cast void pointer into Type pointer in C++ in #define in case of type is not given (C-style) [MSVS]

前端 未结 4 933
暗喜
暗喜 2021-02-15 15:00

Hi!

I\'ve used the following C macro, But in C++ it can\'t automatically cast void* to type*

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-15 15:18

    I do not recommend doing this; this is terrible code and if you are using C you should compile it with a C compiler (or, in Visual C++, as a C file)

    If you are using Visual C++, you can use decltype:

    #define MALLOC_SAFE(var, size)                      \
    {                                                   \
        var = static_cast(malloc(size)); \
        if (!var) goto error;                           \
    }
    

提交回复
热议问题