J
Joel C. Salomon
Untested, but I think this should work:
#ifdef __cplusplus
#define const_cast(T, exp) const_cast<T>(exp)
#else
#define const_cast(T, exp) _Generic((exp), const T : (T)(exp))
#endif
void foo(void) {
const int i = 42;
// &i is compatible with `const int *` = `int const *`
int *pi = const_cast(int *, &i);
// pi is compatible with `const int const *` = `int const *`
int const *pci = const_cast(int const *, pi);
// fails: types incompatible
double *pd = const_cast(double *, &i);
} /* d--n google groups*/
Disgusting, no?
(Even if this works, it won't be too hard to make it break. Typedef'd pointers will do it: `const vptr` = `void * const` != `void const *`.)
I've got another yucky hack brewing: a reinterpret_cast that casts everything *except* qualifiers.
--Joel
#ifdef __cplusplus
#define const_cast(T, exp) const_cast<T>(exp)
#else
#define const_cast(T, exp) _Generic((exp), const T : (T)(exp))
#endif
void foo(void) {
const int i = 42;
// &i is compatible with `const int *` = `int const *`
int *pi = const_cast(int *, &i);
// pi is compatible with `const int const *` = `int const *`
int const *pci = const_cast(int const *, pi);
// fails: types incompatible
double *pd = const_cast(double *, &i);
} /* d--n google groups*/
Disgusting, no?
(Even if this works, it won't be too hard to make it break. Typedef'd pointers will do it: `const vptr` = `void * const` != `void const *`.)
I've got another yucky hack brewing: a reinterpret_cast that casts everything *except* qualifiers.
--Joel