【问题标题】:Is codecvt not a std header?codecvt 不是标准头吗?
【发布时间】:2013-03-25 12:36:30
【问题描述】:

此代码使用 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


【解决方案1】:

GCC拒绝这段代码的原因很简单:libstdc++还不支持&lt;codecvt&gt;

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

22.5 标准代码转换方面 N

【讨论】:

  • 不支持&lt;codecvt&gt;似乎很正常,因为标准中没有这样的文件。
  • @JamesKanze:有。你认为“22.5”指的是什么?这是定义&lt;codecvt&gt; 的部分。
  • 另一个 C++11 特性,我明白了。 (简单地定义名称并使用现有设施会更有意义。)
  • 嗨。我仍在寻找解决方法。有人有什么建议吗?
  • @Hugues 和 Swtsvn:我遇到了同样的问题并使用 boost.locale 解决了它:请参阅下面的答案。
【解决方案2】:

这个问题是在大约 3 年前提出的,我很惊讶我在使用带有新更新的 Ubuntu 14.04 时也遇到了同样的问题。

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

22.5 标准代码转换方面Y

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

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

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

...

用于 Unicode 转换的语言环境方面;

...

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

【讨论】:

    【解决方案3】:

    使用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());
    }  
    

    【讨论】:

    • 您知道任何免升压解决方案吗?
    • 除此线程中的其他答案外没有其他答案,恐怕。我希望您有一个不再需要此解决方法的最新标准库实现。
    • 只有 Gcc4.8.5 用于 bazel。
    • @Nikos 是的,但我们只有 4.8.5 :-(
    【解决方案4】:

    它适用于带有 gcc 5.3 的 Ubuntu 14.04。

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多