简体   繁体   中英

std::cout.imbue() multiple call

Compiler : Visual Studio 2019(C++20, latest)
I have a question about std::locale
Is it impossible to call std::cout.imbue multiple?
Why is it impossible?

When I ran below code.. I can see two of question mark(??) after second imbue function call..

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

int main()
{
    auto loc = std::locale("de_DE.utf8");
    std::cout.imbue(loc);
    std::cout << "locale_name = " << std::cout.getloc().name() << std::endl;
    std::cout << 100.50 << std::endl;
    std::cout << std::showbase << std::put_money(1050) << std::endl;

    std::cout.imbue(loc);   // no output below
    std::cout << "locale_name = " << std::cout.getloc().name() << std::endl;
    std::cout << 100.50 << std::endl;
    std::cout << std::showbase << std::put_money(1050) << std::endl;
}

Here is output
locale_name = de_DE.utf8
100,5
10,50 ??

There is no such restriction, and the output for me is:

locale_name = de_DE.utf8
100,5
10,50 €
locale_name = de_DE.utf8
100,5
10,50 €

Note that the Euro symbol may be replaced by weird stuff like ?? if your output terminal is not properly configured to show UTF-8 characters, or if that glyph is missing in your terminal's font.

For example, from memory, Visual Studio's output console either does not support Unicode or always decodes as UTF-16. One of the two. Probably the latter.

Besides that, if you get output different from the above, that would be an interesting standard library bug in your implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM