codecvt不是std头吗?

问题描述 投票:41回答:4

此代码使用Visual C ++ 11进行编译,并在Windows 7上按预期运行,但无法使用Windows 7上的MinGW 4.7.0或Linux上的gcc 4.8.0进行编译。使用-std=c++11标志进行编译

#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:没有这样的文件或目录。

c++ c++11
4个回答
29
投票

GCC拒绝此代码的原因很简单:libstdc ++还不支持<codecvt>

C++11 support status page证实了这一点:

22.5标准代码转换方面N.


21
投票

大约3年前问了这个问题,我很惊讶我使用Ubuntu 14.04也遇到了同样的问题。

第二个惊喜,@ Fanael提供的链接现在显示:

22.5标准代码转换方面Y.

所以我搜索了哪个版本的GCC将完全实现C ++ 11。事实证明,在GCC 5中增加了全面支持:

https://gcc.gnu.org/gcc-5/changes.html

完全支持C ++ 11,包括以下新功能:

...

用于Unicode转换的区域设置方面;

...

如果我有足够的声誉,我会很乐意对答案发表评论:)


19
投票

使用Boost.Locale的解决方法:

#include <boost/locale/encoding_utf.hpp>
#include <string>

using boost::locale::conv::utf_to_utf;

std::wstring utf8_to_wstring(const std::string& str)
{
    return utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}

std::string wstring_to_utf8(const std::wstring& str)
{
    return utf_to_utf<char>(str.c_str(), str.c_str() + str.size());
}  

2
投票

它适用于Ubuntu 14.04和gcc 5.3。

© www.soinside.com 2019 - 2024. All rights reserved.