std::toupper(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

std::toupper πŸ’…

okay so this is the C++ Standard Library manual page for std::toupper, and we're gonna make it make sense, bestie. ✨


🌸 NAME 🌸

std::toupper β€” std::toupper (the name section is just doing its little mirror selfie, no notes).


✨ Synopsis ✨

Defined in header <cctype>:

int toupper( int ch );

This icon converts the given character to uppercase according to the character conversion rules defined by the currently installed C locale. Slay.

In the default "C" locale, the following lowercase letters abcdefghijklmnopqrstuvwxyz are replaced with the respective uppercase letters ABCDEFGHIJKLMNOPQRSTUVWXYZ. Cute.


πŸŽ€ Parameters πŸŽ€

  • ch β€” the character to be converted. If the value of ch is not representable as unsigned char and does not equal EOF, the behavior is undefined. Not "oopsie" undefined, actual undefined, so don't play with that πŸ’€.

πŸ’– Return value πŸ’–

The converted character, or ch if no uppercase version is defined by the current C locale. That's it, that's the return.


πŸ¦‹ Notes πŸ¦‹

Like all other functions from <cctype>, the behavior of std::toupper is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char:

char my_toupper(char ch)
{
    return static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
}

Similarly, they should not be directly used with standard algorithms when the iterator's value type is char or signed char. Instead, convert the value to unsigned char first:

std::string str_toupper(std::string s)
{
    std::transform(s.begin(), s.end(), s.begin(),
                // static_cast<int(*)(int)>(std::toupper)         // wrong
                // [](int c){ return std::toupper(c); }           // wrong
                // [](char c){ return std::toupper(c); }          // wrong
                   [](unsigned char c){ return std::toupper(c); } // correct
                  );
    return s;
}

πŸ”₯ Example πŸ”₯

// Run this code

#include <cctype>
#include <clocale>
#include <iostream>

int main()
{
    unsigned char c = '\xb8'; // the character ΕΎ in ISO-8859-15
                              // but ΒΈ (cedilla) in ISO-8859-1

    std::setlocale(LC_ALL, "en_US.iso88591");
    std::cout << std::hex << std::showbase;
    std::cout << "in iso8859-1, toupper('0xb8') gives " << std::toupper(c) << '\n';
    std::setlocale(LC_ALL, "en_US.iso885915");
    std::cout << "in iso8859-15, toupper('0xb8') gives " << std::toupper(c) << '\n';
}

⭐ Output ⭐

in iso8859-1, toupper('0xb8') gives 0xb8
in iso8859-15, toupper('0xb8') gives 0xb4

πŸ‘‘ See also πŸ‘‘

  • tolower β€” converts a character to lowercase (function).
  • toupper(std::locale) β€” converts a character to uppercase using the ctype facet of a locale (function template).
  • towupper β€” converts a wide character to uppercase (function).
  • C documentation for toupper β€” yes bestie, the C docs are right there in the see-also list.

kisses and uppercase slay πŸ’‹ this page is from http://cppreference.com, dated 2024.06.10, and it's the std::toupper(3) manual page out 🫢✨

πŸ“¦ where this page comes from