Talk:cpp/string/byte/toupper
From cppreference.com
< Talk:cpp | string/byte
Are you sure about this UB?
The C++ standard doesn't say anything other than to defer to C (http://eel.is/c++draft/c.strings#2). And the C standard says it shall not result in UB. (7.4.2.2) So, cppreference.com is not correct. <--- quoting other people's opinions about this article.
This point seems strong enough that I'm going forth and removing the note about UB. Please consider checking your descriptions of other functions from cctype for similar errors. (std::tolower maybe?)
- The behavior is undefined per C11's 7.4p1 "In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined". This has been there since C89 (4.3 in ANSI numbering) --Cubbi (talk) 07:26, 28 January 2016 (PST)
[edit] toupper(EOF) and other doubts
Is toupper(EOF) guaranteed to return EOF? Also, should we mention that the ctype functions can actually be macros in C? What about C++? I seem to remember a general rule that the C++ functions must be real functions.
- yes on all counts: C11 7.4.2.2 says "If the argument is character for which...; otherwise the argument is returned unchanged" (EOF is not even a character, definitely not a character for which...). As for macros, C11's 7.1.4 says "Any function declared in a header may be additionally implemented as a function-like macro defined in the header ... an implementation shall provide an actual function for each library function, even if it also provides a macro for that function", and yes, C++ does not support that: C++17 20.5.1.2[headers]p4 "Names that are defined as functions in C shall be defined as functions in the C++ standard library ... This disallows the practice, allowed in C, of providing a masking macro in addition to the function "
- ..except regarding "should we mention..."? - it would be silly to write on every single C library page "this function may also be accompanied by a masking function-like macro". Some top-level page might mention that though. --Cubbi (talk) 05:45, 28 July 2017 (PDT)