I was dealing with a platform specific bug today in which on a Windows machine a certain string would be quite garbled, but not on a Mac. The bug had to do with several lines that did both explicit and implicit conversions between std::string and const char *. Basically, I had a function with the signature
void foo(const std::string &id);
where foo at some point prints the string. On Windows, if called like below it would print the id string with various levels of corruption (garbling the first few characters or as much as the whole string)
std::string mystring = bar();
const char *id = mystring.c_str();
foo(id); // pass the C style string in because I thought that's what it took
I corrected the error by calling foo correctly:
std::string mystring = bar();
foo(mystring);
I can't figure out a few things though, like
- What was the source of the bug?
- Why was it platform specific?
- Is implicit conversion between
const char *andstd::stringever safe?