ぷろみん

プログラミング的な内容を扱ってます

C++17からはwstring_convertが非推奨となるので環境毎のコードが推奨される

概要

推奨されるマルチバイト文字変換。

非推奨

C++17からスタンダードライブラリのものは非推奨になる

cpprefjp.github.io

Visual Studioで下記のようなコードをC++17を有効にした状態で書くと

#include <codecvt>
#include <locale>

int main()
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
}

エラー C4996 'std::codecvt_utf8<wchar_t,1114111,0>': warning STL4017: std::wbuffer_convert, std::wstring_convert, and the header (containing std::codecvt_mode, std::codecvt_utf8, std::codecvt_utf16, and std::codecvt_utf8_utf16) are deprecated in C++17. (The std::codecvt class template is NOT deprecated.) The C++ Standard doesn't provide equivalent non-deprecated functionality; consider using MultiByteToWideChar() and WideCharToMultiByte() from <Windows.h> instead. You can define SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING or SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.

上記のようなエラーが出る。

推奨

それをふまえて、windows環境で推奨されるマルチバイト変換は下記のようになるだろう。

stackoverflow.com

std::basic_string::dataメソッドの知名度が低い。