The question has changed after this answer was posted, adding that the strings are hardcoded literals to be used in SQL queries. For that simple u8
strings are a simple solution, and parts answered here become irrelevant. I'm not going to chase the question through this or further changes.
Re
” I want to use string encoded in the UTF-8 (I'm sorry if its a bad wording, please correct me so I understand what is a proper one). Also, I want my program to be cross-platform.
Then you're plain out of luck.
Microsoft's documentation explicitly states that their setlocale
does not support UTF-8:
MSDN docs on setlocale
:
” The set of available locale names, languages, country/region codes, and code pages includes all those supported by the Windows NLS API except code pages that require more than two bytes per character, such as UTF-7 and UTF-8. If you provide a code page value of UTF-7 or UTF-8, setlocale
will fail, returning NULL
.
Heads-up: in spite of the fact that It Does Not Work™, and is explicitly documented as not working, there are numerous web sites and blogs, probably even books, that recommend the approach, in a sort of ostrich-like way. They often look authoritative. But the info is rubbish.
Re
” what is the best way to create a UTF8 representation of std::{w}string with the least possible conditional code?
That depends on what you have available. The standard library offers std::codecvt
. It's been asked about and answered before, e.g. (Convert wstring to string encoded in UTF-8).