4 ms·
Note: When converting void* to T* you can use use static_cast, which is well-defined. For example int* x = static_cast<int*>(malloc(sizeof(int)));
by mortehu 8y ago
Note: When converting void* to T* you can use use static_cast, which is well-defined. For example
int* x = static_cast<int*>(malloc(sizeof(int)));
- kazinator 8y agoBTW, I find this useful when programming in a C dialect that compiles as C: #ifdef __cplusplus #define strip_qual(TYPE, EXPR) (const_cast<TYPE>(EXPR)) #define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR)) #define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR)) #else #define strip_qual(TYPE, EXPR) ((TYPE) (EXPR)) #define convert(TYPE, EXPR) ((TYPE) (EXPR)) #define coerce(TYPE, EXPR) ((TYPE) (EXPR)) #endif Very cool; you just use these macros for all your casts, and when building using a C++ compiler, you get additional diagnostics, like that you're accidentally stripping away a const-qualifier.
- pjmlp 8y agoYou just re-invented MFC macros for the C++ features that were being discussed for ISO C++89. https://docs.microsoft.com/en-us/cpp/mfc/reference/mfc-macros-and-globals https://docs.microsoft.com/en-us/cpp/mfc/reference/mfc-macro...