質問

I am wondering is this adequate enough to use in a production codebase?

std::wstring s2w(const std::string& s)
{
    std::wstring ws;
    ws.assign(s.begin(), s.end());
    return ws;
}

std::string w2s(const std::wstring& w)
{
    std::string s;
    s.assign(w.begin(), w.end());
    return s;
}
役に立ちましたか?

解決

No, this is not sufficient.

Consider a wstring that contains the following characters (numeric values used for clarity) { 0x41, 0x1243, 0x62 } (That's 'A', followed by ETHIOPIC SYLLABLE QAA, followed by 'b' if you're using Unicode).

Converting to a string using your routine would result in a sequence containing { 0x41, 0x43, 0x42 }, or 'ACb'. Probably not what you expected - or wanted.

As DyP said - you're not doing any conversion, only narrowing.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top