Joel C. Salomon
5/20/2011 11:29:00 PM
On 05/20/2011 06:48 PM, Jens Thoms Toerring wrote:
> Joel C. Salomon <joelcsalomon@gmail.com> wrote:
>> 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
>
> What's '_Generic' meant to do? Without knowing that there's no
> way to figure out what this is meant to do (or what you expect
> it to do).
If `exp` is type-compatible with `const T` -- i.e., `exp` is itself of
type `T` or `const T` -- then cast it to type T. Otherwise the (C1x)
compiler will complain of incompatible types.
As the `#ifdef __cplusplus` documents, this is *intended* to be a
work-alike clone of C++'s `const_cast<>`.
>> I've got another yucky hack brewing: a reinterpret_cast that casts
>> everything *except* qualifiers.
>
> I'm not yet convinced that dragging C++ stuff into C will
> work (or will be beneficial). A bit more of context of what
> you intent to get from that might be useful...
Oh, I don't know that it's a *good* idea; I was just opening up the
discussion. Given all the arguments that C++ puts forward for its
`*_cast<>` -- findable with grep, control over exactly what is being
cast away, etc. -- and assuming that C1x gives us the tools to do
something similar, what say we come up with a prototype and discuss it?
--Joel