Related articles |
---|
[34 earlier articles] |
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08) |
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-08) |
Re: Bounds checking, Optimization techniques and undefined behavior derek@_NOSPAM_knosof.co.uk (Derek M. Jones) (2019-05-08) |
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-09) |
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-09) |
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-09) |
Re: Bounds checking, Optimization techniques and undefined behavior 847-115-0292@kylheku.com (Kaz Kylheku) (2019-05-10) |
From: | Kaz Kylheku <847-115-0292@kylheku.com> |
Newsgroups: | comp.compilers |
Date: | Fri, 10 May 2019 03:38:48 +0000 (UTC) |
Organization: | Aioe.org NNTP Server |
References: | 19-04-021 19-04-023 19-04-037 19-04-039 19-04-042 19-04-044 19-04-047 19-05-004 19-05-006 19-05-016 19-05-020 19-05-024 19-05-025 19-05-028 19-05-029 19-05-034 19-05-045 19-05-061 |
Injection-Info: | gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="52587"; mail-complaints-to="abuse@iecc.com" |
Keywords: | C, C++, practice, comment |
Posted-Date: | 10 May 2019 10:46:22 EDT |
On 2019-05-08, David Brown <david.brown@hesbynett.no> wrote:
> [I'm looking at C11 and don't see where it says you can't cast away
> const. What am I missing? I agree any approach here is ugly and we're
> near the limits of what you can say in C. -John]
By the way, on a tangent related to this topic, I use the following
definitions in code that compiles both as C and 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
When compiling as C++, I get diagnostics from the more nuanced cast
operators, which reduce to just regular C casts when compiling as C.
For instance, casting away const requires strip_qual(type, val).
That's just one way we can get the benefits of some C++ safety
without leaving C. Another example is the stronger type safety
of enumerations, and the rules around void * conversions.
--
TXR Programming Lanuage: http://nongnu.org/txr
Music DIY Mailing List: http://www.kylheku.com/diy
ADA MP-1 Mailing List: http://www.kylheku.com/mp1
[Wow, that's ugly. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.