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…”

Thererefore: C++17 : codecvt is deprecated. 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 any standard  string type to std::string type here is the solution:

Just one simple lambda.

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

Actually that lambda auto argument will take anything that is a “range”, i.e. “anything” that has  begin() and end(). And of course that “any thing”  must be possible to convert as string of  some types, possible to transform to char.  In any case don’t worry, your compiler will produce an multi screen of errors if that can not be done.

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

No templates, just lambdas. Here is one (almost) comprehensive test:

That’s it. No codecvt in sight. Hint: string literals work too.

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.

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


Leave a Reply