std::toupper(std::locale)

Defined in header <locale>
1
2
template< class charT >
charT toupper( charT ch, const locale& loc );

Converts the character ch to uppercase if possible, using the conversion rules specified by the given locale's std::ctype facet.

Parameters

ch - character
loc - locale

Return value

Returns the uppercase form of ch if one is listed in the locale, otherwise return ch unchanged.

Notes

Only 1:1 character mapping can be performed by this function, e.g. the uppercase form of 'ß' is (with some exceptions) the two-character string "SS", which cannot be obtained by std::toupper.

Possible implementation

1
2
3
4
template< class charT >
charT toupper( charT ch, const std::locale& loc ) {
    return std::use_facet<std::ctype<charT>>(loc).toupper(ch);
}

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cwctype>
#include <locale>
  
int main()
{
    wchar_t c = L'\u017f'; // Latin small letter Long S ('ſ')
  
    std::cout << std::hex << std::showbase;
  
    std::cout << "in the default locale, toupper(" << (std::wint_t)c << ") = "
              << std::toupper(c, std::locale()) << '\n';
  
    std::cout << "in Unicode locale, toupper(" << (std::wint_t)c << ") = "
              << std::toupper(c, std::locale("en_US.utf8")) << '\n';
}

Output:

1
2
in the default locale, toupper(0x17f) = 0x17f
in Unicode locale, toupper(0x17f) = 0x53

See also

converts a character to lowercase using the ctype facet of a locale
(function template)
converts a character to uppercase
(function)
converts a wide character to uppercase
(function)
doc_CPP
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.