Wide/narrow character conversion using input iterators

This approach converts between narrow and wide characters using input iterators.

#include <x/codecvtiter.H>
#include <x/locale.H>

#include <string>
#include <algorithm>

x::locale conv_locale;

std::string str;

std::wstring wstr;

typedef std::string::iterator inp_iter_t;

typedef x::icodecvtiter<inp_iter_t>::ctow_iter_t ctow_inp_iter_t;

ctow_inp_iter_t::iter ins=std::copy(ctow_inp_iter_t::create(str.begin(),
                                                            str.end(),
                                                            conv_locale),
                                    ctow_inp_iter_t::iter(),
                                    std::back_insert_iterator<std::wstring>(wstr));

The template parameter of x::icodecvtiter<inp_iter_t>::ctow_iter_t is any narrow character input iterator class.

The x::icodecvtiter<inp_iter_t>::ctow_iter_t template defines a create() method that returns an iter type, also defined in the template. This instance defines an input iterator. Iterating over this iterator iterates over the narrow input sequence, converts it to wide character sequence, which gets iterated over by this iterator. The first parameter to create() is the beginning iterator for the narrow character sequence, the second parameter is the ending iterator. The optional third parameter specifies the locale for the conversion, and defaults to the global locale.

create() returns an iter that represents the beginning iterator for the converted input sequence. A default constructor for iter produces the ending iterator value for a converted input sequence.

x::icodecvtiter<ins_iter_t>::wtoc_iter_t defines a create() and an iter that implement an analogous input iterator that converts wide characters to narrow characters.

typedef x::icodecvtiter_bytype<ins_iter_t, char, wchar_t>::iter_t ctow_inp_iter_t;

x::icodecvtiter_bytype is an alternative way to select either ctow_iter_t or wtoc_iter_t, depending on the template's second and third parameters.