std::numpun...o_falsename(3)

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

πŸ’… std::numpunct::truename, do_truename, falsename, do_falsename πŸ’…

okay bestie, sit down, grab a straw, because we're talking about std::numpunct::truename, do_truename, falsename, and do_falsename β€” from the C++ Standard Library, no cap. this is the page for std::numpun...o_falsename(3) and it's genuinely so much more fun than the name makes it sound, i promise 🫢

it's all about what your program prints when it has to spell out true and false β€” the literal words. you know how std::boolalpha makes booleans print as words instead of 1 and 0? yeah. these four functions are the ones picking those words. and you can absolutely customize them. slay.


✨ NAME ✨

std::numpunct::truename, do_truename, falsename, do_falsename β€” literally just the four of them, together, holding hands πŸŽ€


πŸ¦‹ Synopsis πŸ¦‹

defined in header <locale>, bestie. don't forget it or you'll be sad:

public:                                   (1)
string_type truename() const;
public:                                   (2)
string_type falsename() const;
protected:                                (3)
virtual string_type do_truename() const;
protected:                                (4)
virtual string_type do_falsename() const;

okay so what are these actually doing:

  1. and 2. these are the public member functions, and here's the little secret β€” they call the member function do_truename and do_falsename of the most derived class respectively. yes, the public ones are basically the hype people, the protected ones do the real work πŸ’…
  2. returns the string to be used as the representation of the boolean value true. iconic.
  3. returns the string to be used as the representation of the boolean value false. also iconic.

πŸ‘‘ Return value πŸ‘‘

  • 1, 3) β€” the object of type string_type to use as the representation of true. and for the standard specializations of std::numpunct, that's "true" and L"true".
  • 2, 4) β€” the object of type string_type to use as the representation of false. and for the standard specializations of std::numpunct, that's "false" and L"false".

so yeah, out of the box you get the english words. but the whole point is you can remix them 🌈


🌸 Example 🌸

(this one is a whole vibe, watch what happens when we redefine the words)

// Run this code

#include <iomanip>
#include <iostream>
#include <locale>

struct custom_tf : std::numpunct<char>
{
    std::string do_truename()  const { return {'t'}; }
    std::string do_falsename() const { return {'f'}; }
};

int main()
{
    std::cout << std::boolalpha;

    // default boolalpha output
    std::cout << "Default locale,\n"
                 "  boolalpha  true: " << true << "\n"
                 "  boolalpha false: " << false << "\n\n";

    // with custom_tf applied to locale
    std::cout.imbue(std::locale(std::cout.getloc(), new custom_tf));
    std::cout << "Locale with modified numpunct,\n"
                 "  boolalpha  true: " << true << "\n"
                 "  boolalpha false: " << false << '\n';
}

⭐ Output ⭐

Default locale,
  boolalpha  true: true
  boolalpha false: false

Locale with modified numpunct,
  boolalpha  true: t
  boolalpha false: f

see how it went from true/false to just t/f?? the way that inheritance just ate 😭✨ custom do_truename() and do_falsename() overrides and boom β€” whole new personality for your booleans.


ngl bestie, the detail that matters here is that numpunct is a facet you can imbue into a locale, and truename/falsename are the public doors, do_truename/do_falsename are the protected guts. don't mix them up, mkay? πŸ™ˆ

alright that's the whole page, no filler, all facts 🧚 this was brought to you by cppreference.com, version 2024.06.10 β€” go off, and remember: your booleans deserve cute names too. periodt. πŸ’–πŸŽ€

πŸ“¦ where this page comes from