Apache C++ Standard Library User's Guide

## 40.4 Error Indication in Code Conversion Facets

Since file stream buffers depend on their locale's code conversion facet, it is important to understand how they communicate. On writing to the external device, the file stream buffer hands over the content of its internal character buffer, partially or entirely, to the code conversion facet; that is, to its `out()` function. It expects to receive a converted character sequence that it can write to the external device. The reverse takes place, using the `in()` function, on reading from the external file.

In order to make the file stream buffer and the code conversion facet work together effectively, it is necessary that the two main functions `in()` and `out()` indicate error situations the way the file stream buffer expects them to do it.

There are four possible return codes from the member functions `in()` and `out()`:

- `std::codecvt_base::ok`, which should obviously be returned when the conversion went fine.

- `std::codecvt_base::partial`, which should be returned when the code conversion reaches the end of the input sequence `[from,from_end)` before a new character can be created. The file stream buffer's reaction to `partial` is to provide more characters and call the code conversion facet again, in order to successfully complete the conversion.

(In our example of a conversion between ASCII and EBCDIC, we have no reason to ever return `partial`, because this is a conversion of single byte characters. Either a character can be recognized and converted, or the conversion fails; that is, error is returned. The `partial` return code only makes sense in wide-character and multibyte conversions.)

- `std::codecvt_base::error`, which indicates a violation of the conversion rules; that is, the character sequence to be converted does not obey the expected rules and thus cannot be recognized and converted. In this situation, the file stream buffer stops doing anything, and the file stream eventually sets its state to `badbit` and throws an exception if appropriate.

- `std::codecvt_base::noconv`, which is returned if no conversion was needed.