Re:Torvalds: Standards need to be questioned
by mortehu on Tuesday June 05, 2018 @06:46PM
(#17242765)
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)));
Re:Torvalds: Standards need to be questioned
by kazinator on Tuesday June 05, 2018 @07:21PM
(#17243010)
BTW, 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.
Re:Torvalds: Standards need to be questioned
by pjmlp on Wednesday June 06, 2018 @03:17AM
(#17244984)
You 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-macro...
Short of converting void* (or sufficiently large ints) to T* because of legacy API, I'm not sure reinterpret_cast has any portable use. But I'm sure you can NOT type pun with it if that would break the aliasing rules.