Reputation: 1737
I have the following code as a private function in a string class I'm writing (it's "borrowed" from wstring_convert with some modifications done by me):
template <typename T>
std::string my_string::to_bytes(const typename T::intern_type *_First, const typename T::intern_type *_Last) const
{
const T* p_conv = &std::use_facet<T>(m_loc);
...
switch (p_conv->out(_State0, _First, _Last, _First, _Dest, _Dest + _Bbuf.size(), _Dnext))
{
...
}
}
m_loc
is a global std::locale containing the system locale.
The function as such is not the problem, the problem is that when I call the function like this:
const wchar_t *_Wptr = wstr.c_str();
m_utf8 += to_bytes<codecvt_utf8<wchar_t>>(_Wptr, _Wptr + wstr.size());
it doesn't actually use codecvt_utf8<wchar_t>
(which converts between native wchar_t and utf-8) but instead codecvt<wchar_t, char>
(which converts between native wchar_t and native char). I can't for the life of me understand why this happens. Obviously I must do something wrong somewhere and I can admit the locale system with all its facets and whatnots is confusing me a bit.
I guess it's a problem related to use_facet and my locale in some way. Maybe the locale can't create the utf-8 facet? If that sentence makes sense? Maybe I'm not using use_facet correctly?
Upvotes: 0
Views: 576
Reputation: 47448
codecvt_utf8
is a standalone, locale-independent facet, provided by the standard library. There is no need to use use_facet to access it.
What you get from use_facet<T>
is whatever facet is in your locale with the id T::id, which happens to be its codecvt
Upvotes: 1