Appending a codepoint to an UTF8 std::string using icu4c My code is
void utf8_append(UChar32 cp, std::string&amp; str) {
    size_t offset = str.size();
    str.resize(offset + U8_LENGTH(cp));
    auto ptr = reinterpret_cast&lt;uint8_t*&gt;(&amp;str[0]);
    U8_APPEND_UNSAFE(ptr, offset, static_cast&lt;uint32_t&gt;(cp));
}

This works but seems ugly. Maybe I am overlooking a simpler approach?
Relevant documentation: https://unicode-org.github.io/icu/userguide/strings/utf-8.html and https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/utf8_8h.html.
Beauty is in the eye of the beholder. I say it is perfectly valid and correct code! The only thing you might get rid of is the static_cast&lt;uint32_t&gt;, as an UChar32, which is an alias forint32_t, will implicitly cast to uint32_t without warnings.
You could also use append() instead of resize(), avoiding the addition, and remove the temporary ptr, to finally get:
void utf8_append(UChar32 cp, std::string&amp; str) {
    auto offset = str.size();
    str.append(U8_LENGTH(cp), {});
    U8_APPEND_UNSAFE(reinterpret_cast&lt;uint8_t *&gt;(&amp;str[0]), offset, cp);
}

If you can use C++17, str.data() is slightly nicer than &amp;str[0] in my opinion. Or you could write &amp;str.front().