zxc*_*cat 7 c c++ casting void-pointers visual-c++
嗨!
我使用了以下C宏,但在C++中它无法自动转换void*为type*.
#define MALLOC_SAFE(var, size) { \
var = malloc(size); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
我知道,我可以这样做:
#define MALLOC_SAFE_CPP(var, type, size) { \
var = (type)malloc(size); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
但我不想重写大部分代码,MALLOC_SAFE使用的地方.
有没有办法在没有给宏的类型的情况下这样做?也许一些MSVC 2005 #pragma/__declspec/其他?
ps:我不能使用C编译器,因为我的代码是大项目的一部分(数百个模块之一).现在它是在C++上.我知道,我可以单独构建我的代码.但这是旧代码,我只是想快速移植它.
问题是关于void*cast;)如果不可能,我只需用MACRO_SAFE_CPP替换MACRO_SAFE
谢谢!
GMa*_*ckG 34
为了让詹姆斯的回答更加肮脏,如果你没有decltype支持,你也可以这样做:
template <typename T>
class auto_cast_wrapper
{
public:
template <typename R>
friend auto_cast_wrapper<R> auto_cast(const R& x);
template <typename U>
operator U()
{
return static_cast<U>(mX);
}
private:
auto_cast_wrapper(const T& x) :
mX(x)
{}
auto_cast_wrapper(const auto_cast_wrapper& other) :
mX(other.mX)
{}
// non-assignable
auto_cast_wrapper& operator=(const auto_cast_wrapper&);
const T& mX;
};
template <typename R>
auto_cast_wrapper<R> auto_cast(const R& x)
{
return auto_cast_wrapper<R>(x);
}
Run Code Online (Sandbox Code Playgroud)
然后:
#define MALLOC_SAFE(var, size) \
{ \
var = auto_cast(malloc(size)); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
我在我的博客上扩展了这个实用程序(在C++ 11中).不要用它来做任何事情,除了邪恶.
Jam*_*lis 13
我不建议这样做; 这是可怕的代码,如果您使用C,您应该使用C编译器(或者,在Visual C++中,作为C文件)编译它
如果您使用的是Visual C++,则可以使用decltype:
#define MALLOC_SAFE(var, size) \
{ \
var = static_cast<decltype(var)>(malloc(size)); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
例如,像这样:
template <class T>
void malloc_safe_impl(T** p, size_t size)
{
*p = static_cast<T*>(malloc(size));
}
#define MALLOC_SAFE(var, size) { \
malloc_safe_impl(&var, size); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)