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

前端 未结 4 921
暗喜
暗喜 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:22

    For example, like this:

    template 
    void malloc_safe_impl(T** p, size_t size)
    {
        *p = static_cast(malloc(size));
    }
    
    #define MALLOC_SAFE(var, size) { \
        malloc_safe_impl(&var, size); \
        if (!var) goto error; \
    }
    

提交回复
热议问题