std::wstrin...t::converted(3)

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

🌸 NAME 🌸

okay so bestie, this page is std::wstring_convert::converted β€” and yes, the NAME line is literally std::wstring_convert::converted - std::wstring_convert::converted, because sometimes the manual just repeats itself and we respect it πŸ’…. It’s from the C++ Standard Libary manual (spelling exactly as the page has it, no cap), and the page title is std::wstrin...t::converted(3).


✨ Synopsis ✨

Defined in header <locale>:

std::size_t converted() const noexcept;

So this is the little member function that returns the number of source characters that were processed by the most recent from_bytes() or to_bytes() β€” lowkey the receipts for your last conversion, fr fr.


πŸ’– Return value πŸ’–

It returns the number of characters consumed by the most recent conversion operation. That’s the whole vibe: last conversion in, character count out ✨.


🧚 Example 🧚

The page says // Run this code, and then gives you this absolute icon of a snippet:

        #include <codecvt>
        #include <iostream>
        #include <locale>
        #include <string>

        int main()
        {
            std::string utf8 = "z\u00df\u6c34\U0001d10b"; // or u8"zΓŸζ°΄π„‹"
                           // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
            std::cout << "original UTF-8 string size: " << utf8.size() << '\n';

            // the UTF-8 - UTF-32 standard conversion facet
            std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cvt;

            // UTF-8 to UTF-32
            std::u32string utf32 = cvt.from_bytes(utf8);
            std::cout << "UTF-32 string size: " << utf32.size() << '\n';
            std::cout << "converted() == " << cvt.converted() << '\n';
            // UTF-32 to UTF-8
            utf8 = cvt.to_bytes(utf32);
            std::cout << "new UTF-8 string size: " << utf8.size() << '\n';
            std::cout << "converted() == " << cvt.converted() << '\n';
        }

🌈 Output 🌈

        original UTF-8 string size: 10
        UTF-32 string size: 4
        converted() == 10
        new UTF-8 string size: 10
        converted() == 4

Not the output serving looks, bestie 😭 β€” the counts are just doing their little conversion thing and we love that for them 🫢.


πŸ’… Defect reports πŸ’…

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. This is the β€œfix it in post” section, and yes it still counts:

  • DR: LWG 2174
  • Applied to: C++11
  • Behavior as published: wstring_convert::converted was not required to be noexcept
  • Correct behavior: required

πŸ¦‹ See also πŸ¦‹

  • to_bytes β€” converts a wide string into a byte string (public member function)
  • from_bytes β€” converts a byte string into a wide string (public member function)

That’s the whole page, bestie! Source: http://cppreference.com; version/date: 2024.06.10; page: std::wstrin...t::converted(3). xoxo, go convert something cute πŸ’–πŸŽ€

πŸ“¦ where this page comes from