|
Boost : |
From: Jan Langer (jan_at_[hidden])
Date: 2002-03-03 15:15:03
hello
the filesystem library will need a mechanism for converting different
char types into other char types. the same problem also occurs with
basic_string. i think this is a quite genaral case and it is worth a
general solution. an other example is a program using wstring and
wanting to print to a normal char-stream.
a reasonable way of solving this is to get a ctype facet and to apply
narrow or widen to the character.
dietmar kuehl wrote a initial version for the dir_it lib. now i have
changed a few things and its IMO a feature which should be added to
the conversion library.
it mainly consists of two functions
template <typename cTo, typename cFrom>
basic_string <cTo> string_cast (basic_string <cFrom> const &);
template <typename cTo, typename cFrom>
cTo char_cast (cFrom);
these functions call a static member function in a class template which
resides in namespace detail and is specialized to do the right thing in
each case. eg. to call narrow if cTo is char.
optionally both functions take a const reference to the locale which
will be used to get the correct ctype facet. the default locale is
locale ().
the string_cast is also overloaded for "cFrom const *" to allow that a
normal c-string is passed.
this type of conversion will be used in the filesystem lib and it will
go into the detail namespace if it turns out that it is not put in any
other part of boost. it will also be necessary to adopt it to other
compilers. i have only tested it with gcc 3.0.3. but it should also
compile with gcc 2.95, since the code for missing locale and ctype is
already there.
i uploaded the file to yahoogroups/files-section as
string_conversion.hpp
regards
jan
-- jan langer ... jan_at_[hidden] "pi ist genau drei"
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk