3

This code:

std::cout << "std::cout for 2112 =  " << 2112 << '\n';
std::stringstream ss;
ss << 2112;
std::cout << "std::stringstream for 2112 =  " << ss.str() << '\n';
YAML::Emitter out;
out << 2112;
std::cout << "Emitter for 2112 = " << out.c_str() << '\n';

produces this output:

std::cout for 2112 =  2112
std::stringstream for 2112 =  2,112
Emitter for 2112 = 2,112

Since Emitter uses std::stringstream, it seems this is where it is coming from. So I want to imbue this with, say, std::locale{"C"}, but how can I tell yaml-cpp to do this? I could convert the numbers (including dealing with vectors, etc., loosing yaml-cpp's convenience operator<< overloads) to a string first and send this to the Emitter, but is there a simpler way to prevent the thousands separator from being included in the yaml string?

YAML::Emitter has a constructor that takes a std::ostream as parameter, which becomes the underlying stream wrapped by the Emitter. Unfortunately, setting the locale of this underlying stream to "C" doesn't solve the problem. In fact, constructing the Emitter with a stringstream results in the Emitter failing to produce any text at all.

6
  • At the end of the day, emitter << 2112 calls Emitter::WriteIntegralType which uses std::stringstream under the hood. Looks like it's locale-bound after all. Commented May 20, 2024 at 7:00
  • Thanks, yes std::stringstream is also inserting the comma. I'll edit my question. Commented May 20, 2024 at 7:25
  • What happens if you do std::locale::global(std::locale("C"));? Commented May 20, 2024 at 11:55
  • 5
    This actually qualifies as a bug. It should not be producing invalid yaml because you have a locale set up Commented May 20, 2024 at 12:40
  • I reported this as a bug. Commented May 20, 2024 at 13:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.