Question: Is there an easy STL way to convert a std::string to a std::u32string, i.e. a basic\_string of char to char32\_t?

This is not a Unicode question.

Answer: 
To initialise a new string:

```
std::u32string s32(s.begin(), s.end());

```

To assign to an existing string:

```
s32.assign(s.begin(), s.end());

```

If the string might contain characters outside the supported range of `char`, then this might cause sign-extension issues, converting negative values into large positive values. Dealing with that possibility is messier; you'll have to convert to `unsigned char` before widening the value.

```
s32.resize(s.size());
std::transform(s.begin(), s.end(), s32.begin(), 
               [](char c) -> unsigned char {return c;});

```

or a plain loop

```
s32.clear();  // if not already empty
for (unsigned char c : s) {s32 += c;}

```
```
s32.resize(s.length());
std::copy(s.begin(),s.end(),s32.begin());

```