cppreference shows these prototypes for strchr()
:
char *strchr( const char *str, int ch ); (1)
/*QChar*/ *strchr( /*QChar*/ *str, int ch ); (2) (since C23)
and offers this explanation for the second version:
Type-generic function equivalent to (1). Let T be an unqualified character object type.
- If
str
is of typeconst T*
, the return type isconst char*
.- Otherwise, if
str
is of typeT*
, the return type ischar*
.- Otherwise, the behavior is undefined.
What is this QChar*
? Is it a new type?
If so, does GCC and/or Clang support it already? Can I use it in my own application code?
For many years, strchr()
(and some other functions) took a const char pointer argument (to a 0 terminated string), and returned a non-const pointer to a location somewhere inside the string (which can be problematic if someone then tries to write to that location though that pointer when it's not supposed to be mutable). The new Qualified Char versions are generic functions that, when passed a const char pointer, return a const char pointer, and when passed a regular char pointer, return the same.
The proposal with rationale and complete list of affected functions is a PDF that can be read here.