C++ : codecvt deprecated. Panic?

P0618RO:  “…The entire header <codecvt> (which does not contain the class codecvt!) is deprecated, as are the utilities wstring_convert and wbuffer_convert. These features are hard to use correctly, and there are doubts whether they are even specified correctly. Users should use dedicated text-processing libraries instead…”

(update: for the comprehensive “all in one” solution please head here )

Therefore: C++17 : codecvt is deprecated. “Text-processing libraries”? Panic?  Please don’t. My advice is to “stay cool calm and collected and all things will fall into place”. Read on.

In case you want to transform from let’s say wide string type to std::string type here is the standard C++ solution:

Just one simple function. Almost ridiculously simple.

NOTE: This is C++ standard way . For WIN32 aficionados this is apparently not a way. They would need to use  something like cppWINRT to_string.

In case you need to transform to another string type, I am sure you will be more than able to code your solution, using the concept of the code above.

That std::wstring_view argument will take anything that is a wide char string, wide string literals, std::wstring instances, wide string std literals and other wstring views .

Back to subject. My preferred design for transforming to standard string types , would be:

Sixteen little functions. One comprehensive standard solution.

For an advanced version which consists of one function and does all of this, plus any other standard character sequence type please jump here.

Now let us imagine the solution sketched here, is all implemented. Here is one (almost) comprehensive test for transforming to std::string:

In case you have thoughts like: “How do I transform utf8 to utf16 ..”, well in that case, I dare to suggest: you might want to think again.

That’s it. No codecvt in sight. Hint: c++14 introduced, string literals work too.

ps: dbj::print() implementation is trivial for you, I am sure of it. Or just copy/paste my implementation.

Appendix

In standard c++, returning from functions,  one does not need to repeat the type returned. Instead of:

std::string fun (std::wstring str ) {
return std::string{

   str.begin(), str.end()
  };
}

Standard C++ does allow:

std::string fun ( std::wstring str) {
return {

  str.begin(), str.end()
 };
}

Standard C+ compiler already knows std::string is the return type.  One can just type the brace init list without again mentioning the type. This works (as ever) only if there is a constructor required or if there is a user defined conversion.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.