Visual Studio C++ 2015 std::codecvt with char16_t or char32_t(Visual Studio C++ 2015 std::codecvt with char16_t 或 char32_t)
问题描述
这段代码在VS2013下编译OK:
This code compiled OK under VS2013:
std::string Unicode::utf16_to_utf8(std::u16string utf16_string)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(utf16_string);
}
现在使用 VS2015 我得到:
Now with VS2015 I get:
1>unicode.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class std::locale::id std::codecvt<char16_t,char,struct _Mbstatet>::id" (__imp_?id@?$codecvt@_SDU_Mbstatet@@@2V0localeA)
推荐答案
老问题,但供以后参考:这是 Visual Studio 2015 中的一个已知错误,如 此主题 MSDN Social.
Old question, but for future reference: this is a known bug in Visual Studio 2015, as explained in the latest post (January 7th 2016) in this thread of MSDN Social.
您示例的解决方法如下所示(为简单起见,我将您的方法实现为免费函数):
The workaround for your example looks like this (I implemented your method as a free function for simplicity):
#include <codecvt>
#include <locale>
#include <string>
#include <iostream>
#if _MSC_VER >= 1900
std::string utf16_to_utf8(std::u16string utf16_string)
{
std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> convert;
auto p = reinterpret_cast<const int16_t *>(utf16_string.data());
return convert.to_bytes(p, p + utf16_string.size());
}
#else
std::string utf16_to_utf8(std::u16string utf16_string)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(utf16_string);
}
#endif
int main()
{
std::cout << utf16_to_utf8(u"Élémentaire, mon cher Watson!") << std::endl;
return 0;
}
希望这个问题能在以后的版本中得到修复,否则#if
条件需要细化.更新:不,在 VS 2017 中未修复.因此,我已将预处理器条件更新为 >=1900
(最初为 ==1900
).
Hopefully, the problem will be fixed in future releases, otherwise the #if
condition will need refining.
UPDATE: nope, not fixed in VS 2017. Therefore, I've updated the preprocessor conditional to >= 1900
(initially was == 1900
).
这篇关于Visual Studio C++ 2015 std::codecvt with char16_t 或 char32_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Visual Studio C++ 2015 std::codecvt with char16_t 或 char32_t


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09