Re: ptr_fun & tolower confusion
On Jul 4, 10:52 pm, Greg Herlihy <gre...@mac.com> wrote:
On Jul 4, 2:34 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
[...]
Slightly modified from the archive:
#include <tr1/memory>
#include <cstdlib>
#include <locale>
template < typename CharT >
class to_lower {
typedef std::ctype< CharT > char_type;
std::tr1::shared_ptr< std::locale > the_loc_ptr;
char_type const * the_type_ptr;
public:
to_lower ( std::locale const & r_loc = std::locale() )
: the_loc_ptr ( new std::locale ( r_loc ) )
, the_type_ptr ( &std::use_facet< char_type >( *the_loc_ptr ) )
{}
CharT operator() ( CharT chr ) const {
return ( the_type_ptr->tolower( chr ) );
}
};
TR1's shared_ptr<> class is not nearly as useful in this case
as its bind() routine. In fact, calling TR1's bind() would
eliminate the custom to_lower functor and its attendant
complexity.
After all, lowercasing a C++ string seems like it should be a
fairly straightforward task - one that should require only a
few lines of code::
#include <iostream>
#include <string>
#include <algorithm>
#include <locale>
#include <tr1/functional>
using std::locale;
using std::tolower;
using std::tr1::bind;
using std::tr1::placeholders::_1;
int main()
{
std::string s("GrEg");
transform( s.begin(), s.end(), s.begin(),
bind( tolower<char>, _1, locale()));
std::cout << s << "\n";
}
That is, of course, the simplest solution. It hasn't been
available all that long, however, and most of us developed our
solution before bind was available. (The shared_ptr isn't
really necessary here, and even if it was, most of us had simple
implementations of shared_ptr long before it made it into TR1.)
And IMHO, there's nothing wrong with providing a general wrapped
tool (although it does lead to the mistaken belief that you can
generally use tranform for converting to lower case---in
practice, the mapping isn't one to one). And using the ctype
directly will probably be slightly faster (although I doubt that
that is an issue).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34