此代码使用Visual C++ 11编译并在Windows 7上按预期运行,但在Windows 7上使用MinGW 4.7.0或Linux上使用gcc 4.8.0编译失败。
#include <codecvt>
#include <string>
// convert UTF-8 string to wstring
std::wstring utf8_to_wstring (const std::string& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.from_bytes(str);
}
// convert wstring to UTF-8 string
std::string wstring_to_utf8 (const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.to_bytes(str);
}
错误:
codecvt:没有这样的文件或目录。
4条答案
按热度按时间kfgdxczn1#
GCC拒绝此代码的原因很简单:libstdc不支持
<codecvt>
。C11支持状态页确认了这一点:
22.5标准代码转换面N
eufgjt7s2#
这个问题是在3年前问的,我很惊讶我在使用Ubuntu 14.04时也遇到了同样的问题。
第二个惊喜,@Fanael提供的链接现在显示:
22.5标准代码转换面Y
因此,我搜索了哪个版本的GCC将完全实现C11。结果发现GCC 5中添加了完全支持:
https://gcc.gnu.org/gcc-5/changes.html
完全支持C11,包括以下新功能:
...
用于Unicode转换的locale facets;
...
如果我有足够的声誉,我会很乐意对答案发表评论:)
xytpbqjk3#
使用Boost.Locale的解决方法:
ldioqlga4#
它可以在Ubuntu 14.04和gcc 5.3上运行。