【发布时间】:2020-10-15 22:48:13
【问题描述】:
我有以下代码:
std::wstring my_string = L"foo";
std::wstring_convert<std::codecvt_utf8<wchar_t> > my_conv;
const char *chars = my_conv.to_bytes( my_string ).c_str();
转换返回一个空指针(不是NULL,只是"")。
to_bytes 是否返回一个临时指针?
如何正确编写这段代码?
【问题讨论】:
-
to_bytes不返回指向 char 的指针。请发布minimal reproducible example。我不能reproduce这个。 -
@n.'pronouns'm。 '代词' m,我有 MSVC 2017 社区,在 Windows 8.1 上运行。你的环境是什么?
it doesn't return a pointer to char是什么意思? -
它返回 std::string。无论如何,这整个设施已被弃用。
-
@n.'pronouns'm.,它在什么标准中被弃用?以及如何将
std::wstring转换为char *?另外 - 我错过了c_str()的电话。已添加。 -
@Igor
std::wstring_convert在 C++17 以后已弃用