Topic: Inconsistencies in the interface of locale


Author: kanze@gabi-soft.de (James Kanze)
Date: Fri, 25 Apr 2003 18:03:23 +0000 (UTC)
Raw View
I would like to know the logic behind the interface of locale.  In
particular, I am wondering about the template constructor:

    template< typename Facet > locale( locale const&, Facet* ) ;

Why not Facet const* for the second paragraph?  The only way to get an
existing facet is by means of std::use_facet, and this returns a
reference to a const.  The result is that in order to be sure that there
is no code translation during output, you need to do something like:

    typedef std::codecvt< char,
                          std::ofstream::char_type,
                          std::ofstream::traits_type::state_type >
                        CvtFacet ;
    std::locale         fileLocale( ofile.getloc(),
                                    const_cast< CvtFacet* >(
                                        &std::use_facet< CvtFacet >(
                                            std::locale::classic() ) ) ) ;
    ofile.imbue( fileLocale ) ;

I find taking the address of the returned reference a bit awkward
looking, and needing to do a const_cast (which I presume is safe if only
because std::codecvt doesn't have any non-const members) positively
horrible.  What is the rationale behind this?

--
James Kanze             GABI Software             mailto:kanze@gabi-soft.fr
Conseils en informatique orient   e objet/
                           Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, T   l. : +33 (0)1 30 23 45 16

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]