[libc-commits] [libc] [libc] Template StringConverter pop function to avoid duplicate code (PR #152204)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Wed Aug 6 09:50:49 PDT 2025
================
@@ -31,14 +32,24 @@ class CharacterConverter {
bool isEmpty();
bool isValidState();
- size_t sizeAsUTF32();
- size_t sizeAsUTF8();
+ template <typename CharType> size_t sizeAs() {
+ if constexpr (cpp::is_same_v<CharType, char8_t>)
+ return state->total_bytes;
+ else // char32_t
+ return 1; // every character fits in a single char32_t
+ }
----------------
michaelrj-google wrote:
Instead of `if constexpr` you can just use template specialization to select the proper variant
```suggestion
template <typename CharType> size_t sizeAs();
template <> size_t sizeAs<char8_t>() { return state->total_bytes; }
template <> size_t sizeAs<char32_t>() {
return 1; // every character fits in a single char32_t
}
```
https://github.com/llvm/llvm-project/pull/152204
More information about the libc-commits
mailing list