Search code examples
cstringc23

What is the new QChar* in C23?


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 type const T*, the return type is const char*.
  • Otherwise, if str is of type T*, the return type is char*.
  • 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?


Solution

  • 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.