Is codecvt not a std header?
Asked Answered
R

4

45

This code compiles with Visual C++ 11 and runs as expected on Windows 7 but fails to compile using either MinGW 4.7.0 on Windows 7 or gcc 4.8.0 on Linux. Compiling with -std=c++11 flag

#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);
}

Error:

codecvt: No such file or directory.

Relieve answered 25/3, 2013 at 12:36 Comment(1)
Note that the compilers that have implemented C++11 codecvt (e.g. MSVC), are most probably going to deprecate it by this C++17 paper: open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0618r0.html. Unless the "Other Directions" in the paper take precedence.Multifid
B
31

The reason why GCC rejects this code is simple: libstdc++ doesn't support <codecvt> yet.

The C++11 support status page confirms this:

22.5 Standard code conversion facets N

Baxy answered 25/3, 2013 at 12:38 Comment(5)
It would seem normal that it doesn't support <codecvt>, because there is no such file in the standard.Favouritism
@JamesKanze: there is. What do you think that "22.5" refers to? It's the section where <codecvt> is defined.Baxy
Another C++11 feature, I see. (It would have made more sense to simply define the names, and use the existing facilities.)Favouritism
Hi. I am still looking for a workaround. Does anyone have any suggestions ?Fuze
@Hugues and Swtsvn: I had the same problem and solved it using boost.locale: See my answer below.Merrymerryandrew
F
23

The question was asked almost 3 years ago and I was surprised that I am also having same issue using Ubuntu 14.04 with fresh updates.

Second surprise, the link provided by @Fanael shows now:

22.5 Standard code conversion facets Y

So I searched which version of GCC would fully implement C++11. Turns out that full support was added in GCC 5:

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

Full support for C++11, including the following new features:

...

locale facets for Unicode conversion;

...

I would have been happy to put a comment on the answer if I had enough reputation :)

Ferreous answered 16/3, 2016 at 10:17 Comment(0)
M
22

A workaround using 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());
}  
Merrymerryandrew answered 5/3, 2015 at 10:18 Comment(4)
Do you know of any boost-free solution?Nancynandor
None except for the other answers in this thread, I' am afraid. I hope you have a recent standard library implementation available which does no longer require this workaround.Merrymerryandrew
Only got Gcc4.8.5 for bazel.Nancynandor
@Nikos yes, but we only have 4.8.5 :-(Nancynandor
G
2

It works on Ubuntu 14.04 with gcc 5.3.

Guria answered 8/8, 2016 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.