You can't separate a string literal like that. The special sequence inside the quotes is a directive to the compiler to insert the relevant Unicode character at compile time so if you break it into two pieces it is no longer recognized as a directive.
To programatically generate a UTF-16
character based on its Unicode codepoint number you could use the Standard Library Unicode converson functions. Unfortunately there is no direct conversion between UTF-32
(Unicode codepoints) and UTF-16
so you have to go through UTF-8
as an intermediate value:
// UTF-16 may contain either one or two char16_t characters so
// we return a string to potentially contain both.
///
std::u16string codepoint_to_utf16(char32_t cp)
{
// convert UTF-32 (standard unicode codepoint) to UTF-8 intermediate value
char utf8[4];
char* end_of_utf8;
{
char32_t const* from = &cp;
std::mbstate_t mbs;
std::codecvt_utf8<char32_t> ccv;
if(ccv.out(mbs, from, from + 1, from, utf8, utf8 + 4, end_of_utf8))
throw std::runtime_error("bad conversion");
}
// Now convert the UTF-8 intermediate value to UTF-16
char16_t utf16[2];
char16_t* end_of_utf16;
{
char const* from = nullptr;
std::mbstate_t mbs;
std::codecvt_utf8_utf16<char16_t> ccv;
if(ccv.in(mbs, utf8, end_of_utf8, from, utf16, utf16 + 2, end_of_utf16))
throw std::runtime_error("bad conversion");
}
return {utf16, end_of_utf16};
}
int main()
{
std::u16string s; // can hold UTF-16
// iterate through some Greek codepoint values
for(char32_t u = 0x03b1; u < 0x03c9; ++u)
{
// append the converted UTF-16 characters to our string
s += codepoint_to_utf16(u);
}
// do whatever you want with s here...
}