Prev: State of N2801: initializer lists and move semantics
Next: constexpr array function pointers as fast as switch
From: Mathias Gaunard on 5 Jul 2010 10:45 I am the author of a Unicode library that does code conversion at the iterator level, and I would like to adapt my codecs to make them work as codecvt facets usable with file I/O. While it works to subclass std::codecvt<char, char, std::mbstate_t> and std::codecvt<wchar_t, char, std::mbstate_t> for that purpose, it is limiting as I can only provide conversions between char (memory) and char (file) or between wchar_t (memory) and char (file). I would like to be able to create a whole lot of new instances of codecvt: codecvt<char, wchar_t, mbstate_t> codecvt<char32, char, mbstate_t> codecvt<char16, char, mbstate_t> etc. But if I specialize codecvt (inheriting from std::locale::facet and making the foo functions call do_foo), when I use it I get a bad_cast exception thrown at runtime even though the basic_fstream is templated on the same internal type. How is that supposed to work? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 5 Jul 2010 23:44 On Jul 6, 2:45 am, Mathias Gaunard <loufo...(a)gmail.com> wrote: > I would like to be able to create a whole lot of new instances of > codecvt: > codecvt<char, wchar_t, mbstate_t> > codecvt<char32, char, mbstate_t> > codecvt<char16, char, mbstate_t> > etc. I've looked a bit at the implementation of libstdc++, and it seems this simply isn't possible. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Walter van der Hee on 6 Jul 2010 22:31
On 6 July, 16:44, Mathias Gaunard <loufo...(a)gmail.com> wrote: > On Jul 6, 2:45 am, Mathias Gaunard <loufo...(a)gmail.com> wrote: > > > I would like to be able to create a whole lot of new instances of > > codecvt: > > codecvt<char, wchar_t, mbstate_t> > > codecvt<char32, char, mbstate_t> > > codecvt<char16, char, mbstate_t> > > etc. > > I've looked a bit at the implementation of libstdc++, and it seems > this simply isn't possible. > the bad_cast exception is probably thrown from the use_facet function. The basic_fstream calls this function to get the correct facet from the locale. Since it throws, it seems you haven't added your own facets to the locale used by the fstream. regards, Walt. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |