Reading the docs of use_facet
:
The reference returned by this function is valid as long as any
std::locale
object exists that implements Facet.
You're creating a temporary std::locale
in your code, so that temporary is destroyed at the end of the expression, and you're left with a dangling pointer. Like this, it should work:
char s[] = "helloworld";
std::locale en_US("en_US");
const std::collate<char>* pc = &std::use_facet<std::collate<char> >(en_US);
std::string str = pc->transform(s, s + std::strlen(s));
std::cout << str.length() << "," << str << std::endl;