Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 1.53 KB

348.md

File metadata and controls

66 lines (48 loc) · 1.53 KB
Info

  • Did you know that C++26 changed arithmetic overloads of std::to_string and std::to_wstring to use std::format?

Example

int main() {
    setlocale(LC_ALL, "C");
    std::cout << std::to_string(42); // prints 42
    std::cout << std::to_string(.42); // prints 0.42
    std::cout << std::to_string(-1e7); // prints -1e+07
}

https://godbolt.org/z/a7xMEq336

Puzzle

  • Can you add required locale to match expectations?
int main() {
    using namespace boost::ut;
    using std::literals::string_literals::operator""s;

    "locale.to_string (all.us)"_test = [] {
        // TODO
        expect("-1e+07"s == std::to_string(-1e7));
    };

    "locale.to_string (numeric.eu)"_test = [] {
        // TODO
        expect("1,23"s == std::to_string(1.23));
    };
}

https://godbolt.org/z/jocqoM7xd

Solutions

int main() {
    using namespace boost::ut;
    using std::literals::string_literals::operator""s;

    "locale.to_string (all.us)"_test = [] {
        std::setlocale(LC_ALL, "C");
        expect("-1e+07"s == std::to_string(-1e7));
    };

    "locale.to_string (numeric.eu)"_test = [] {
       std::setlocale(LC_NUMERIC, "de_DE.UTF-8");
       expect("1.23"s == std::to_string(1.23)); // env dependent, should be 1,23
    };
}

https://godbolt.org/z/bd3dn6Eqb