Content deleted Content added
Add a better-supported explanation for the inclusion of `const` in the Java language |
Added a note about how the strchr issue could have been worked around in C11. |
||
Line 299:
</syntaxhighlight>
However, in C neither of these is possible{{efn|In
<syntaxhighlight lang=c>char* strchr_m(char *s, int c);
char const* strchr_c(char const *s, int c);
#define strchr(X,Y) _Generic((X), \
char*: strchr_m, \
const char*: strchr_c \
)(X,Y)</syntaxhighlight>C23's actual solution does not use <code>_Generic</code> but is essentially equivalent.}} since C does not have function overloading, and instead, this is handled by having a single function where the input is constant but the output is writable:
<syntaxhighlight lang=cpp>
char *strchr(char const *s, int c);
</syntaxhighlight>
This allows idiomatic C code but does strip the const qualifier if the input actually was const-qualified, violating type safety. This solution was proposed by Ritchie and subsequently adopted. This difference is one of the failures of [[compatibility of C and C++]].
|