std::wstrin...ing_convert(3)

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

🌸 NAME 🌸

okay so bestie, this is the manual page for std::wstring_convert::~wstring_convert πŸ’… β€” that's the destructor of std::wstring_convert, straight out of the C++ Standard Library, and honestly it's giving "one-line wonder, but with lore" ✨ (the page title literally just repeats std::wstring_convert::~wstring_convert, iykyk).


πŸŽ€ Synopsis πŸŽ€

~wstring_convert();

so like, no arguments, no return value β€” it just does its one job: it destroys the wstring_convert object and deletes the pointer to the conversion facet 🫢. that's it. that's the whole vibe check. iconic, honestly.


πŸ’œ Notes πŸ’œ

okay here's where it gets spicy 🌢️ β€” implementations are not all built the same, and you need to know this:

  • some implementations are able to delete any facet, including the locale-specific facets with protected destructors 😌
  • other implementations require the facet to have a public destructor, similar to the locale-independent facets from <codecvt> 😭

this whole situation is LWG issue 721 β€” write that down, bestie πŸŽ€


πŸ¦‹ Example πŸ¦‹

// Run this code πŸƒβ€β™€οΈβœ¨

#include <codecvt>
#include <locale>
#include <utility>

// utility wrapper to adapt locale-bound facets for wstring/wbuffer
convert
template<class Facet>
struct deletable_facet : Facet
{
    template<class ...Args>
    deletable_facet(Args&& ...args) :
Facet(std::forward<Args>(args)...) {}
    ~deletable_facet() {}
};

int main()
{
    // GB18030 / UCS4 conversion, using locale-based facet directly
    // typedef std::codecvt_byname<char32_t, char, std::mbstate_t>
gbfacet_t;
    // Compiler error: "calling a protected destructor of
    //                  codecvt_byname<> in ~wstring_convert"
    // std::wstring_convert<gbfacet_t> gbto32(new
gbfacet_t("zh_CN.gb18030"));

    // GB18030 / UCS4 conversion facet using a facet with public
destructor
    typedef deletable_facet<std::codecvt_byname<char32_t, char,
std::mbstate_t>> gbfacet_t;
    std::wstring_convert<gbfacet_t> gbto32(new
gbfacet_t("zh_CN.gb18030"));
} // destructor called here

so what's the tea β˜•: the deletable_facet struct is your little utility wrapper that adapts locale-bound facets for wstring/wbuffer convert, giving them a public ~deletable_facet() {} so the destructor actually works πŸ’€. the commented-out typedef std::codecvt_byname<char32_t, char, std::mbstate_t> gbfacet_t; block straight-up does not compile β€” you get a compiler error, calling a protected destructor of codecvt_byname<> in ~wstring_convert 😭 β€” because you tried to use the locale-based facet directly. the uncommented version routes through deletable_facet and happily does GB18030 / UCS4 conversion. and yeah, } // destructor called here β€” the destructor gets called right there at the closing brace πŸ‘‘πŸ«Ά


🌈 SEE ALSO 🌈

ngl there's nothing extra listed on this page, bestie β€” this one's a short king πŸ’€

also a lil warning for you: because of the implementation differences up in Notes, whether your facet's destructor is public or protected genuinely changes whether this compiles. don't come for me, come for LWG issue 721 ✨

mwah πŸ’‹ β€” that's the whole page, brought to you by cppreference.com, dated 2024.06.10, and filed under std::wstrin...ing_convert(3) in the C++ Standard Libary (yes that's how the header spells it, no I will not be fixing it, slay πŸŽ€πŸ’–)

πŸ“¦ where this page comes from