+ iconv_codecvt_facet const & facet =
+ use_facet<iconv_codecvt_facet>(os.rdbuf()->getloc());
+
+ // FIXME Changing the codecvt facet of an open file is allowed,
+ // but unsafe for facets that use internal state (see the thread
+ // "iostreams: Does imbue() need to be called before open()?"
+ // in comp.std.c++.
+ // Currently it seems to work with gcc and MSVC, but not with
+ // clang on OS X.
+ // Avoid imbueing with the same encoding again if possible.
+ if (facet.encoding() == e.encoding)
+ return os;
+