std::num_get::~num_get(3)

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

πŸ’… std::num_get::~num_get β€” C++ Standard Library realness (that's the manual, bestie, welcome)

Okay so picture this: you're deep in <locale> land, right? And you need to know what happens when a std::num_get facet gets yeeted out of existence. That's literally this whole page. Let's get into it πŸŽ€


✨ NAME ✨

std::num_get::~num_get β€” and yes, the name is just the name again, that's how these things go, no notes πŸ’€


🌸 Synopsis 🌸

So this thing lives up in here:

Defined in header <locale>

and the signature is giving very much protected-and-private-energy:

protected: ~num_get();

What it does: destructs a std::num_get facet. Simple. Iconic. 🌟

Now for the lore β€” this destructor is protected and virtual (that second part is because the base class destructor is virtual, so it's inherited behavior, iykyk). And because of that protected life, an object of type std::num_get β€” like most facets, fr fr β€” can only be destroyed when:

  • the last std::locale object that implements this facet goes out of scope, or
  • if a user-defined class is derived from std::num_get and implements a public destructor.
Lowkey important: you can't just make a std::num_get and destroy it willy-nilly. The destructor being protected means the compiler will absolutely shut you down if you try. It's giving "you don't have access to this floor" energy 🚫✨

πŸ¦‹ Example πŸ¦‹

Here's the vibe in code β€” run it if you want:

// Run this code

#include <iostream>
#include <locale>

struct Destructible_num_get : public std::num_get<wchar_t>
{
    Destructible_num_get(std::size_t refs = 0) : num_get(refs) {}
    // note: the implicit destructor is public
};

int main()
{
    Destructible_num_get dc;
    // std::num_get<wchar_t> c; // compile error: protected destructor
}

See what she did there? πŸ‘€ She derived a class, gave it a constructor that takes std::size_t refs = 0 and forwards it to num_get(refs), and since the implicit destructor of the derived class is public, Destructible_num_get dc; gets to exist and get destroyed like a normal little object. Main character behavior πŸ’…

But that commented-out line? // std::num_get<wchar_t> c; // compile error: protected destructor β€” that's the cautionary tale. Try it directly and the compiler says absolutely not. Not me getting rejected by the compiler 😭


🫢 Sign-off 🫢

That's the whole page, bestie β€” no cap, that's it. Fresh from http://cppreference.com, dated 2024.06.10, and filed under the very fancy manual name C++ Standard Libary (their spelling, not mine, keeping it exactly as it came to me πŸ’–). Go forth and derive responsibly βœ¨πŸŒˆπŸ‘‘

πŸ“¦ where this page comes from