std::numpun...o_falsename(3)
π 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:
- and 2. these are the public member functions, and here's the little secret β they call the member function
do_truenameanddo_falsenameof the most derived class respectively. yes, the public ones are basically the hype people, the protected ones do the real work π - returns the string to be used as the representation of the boolean value
true. iconic. - returns the string to be used as the representation of the boolean value
false. also iconic.
π Return value π
1, 3)β the object of typestring_typeto use as the representation oftrue. and for the standard specializations ofstd::numpunct, that's"true"andL"true".2, 4)β the object of typestring_typeto use as the representation offalse. and for the standard specializations ofstd::numpunct, that's"false"andL"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 thatnumpunctis a facet you can imbue into a locale, andtruename/falsenameare the public doors,do_truename/do_falsenameare 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms