J
James Kanze
Why not?
template< typename Char, typename Traits >
std::basic_string< Char, Traits >
toLower(
std::basic_string< Char, Traits > const&
in,
std::locale loc = std::locale() )
{
std::basic_string< Char, Traits >
result( in ) ;
std::use_facet< std::ctype< Char > >( loc )
.toupper( &result[ 0 ], &result[ 0 ] + result.size() ) ;
return result ;
}
Why not use the one-liner (assuming t is an std::string)
std::transform(t.begin(),t.end(),t.begin(),std::bind1st(std::mem_fun(&std::ctype<char>::toupper),&std::use_facet<std::ctype< char > >(std::locale())));
Because I like readable code.
That will work as well as any of the others.
If you're compiler can handle it.
Assuming taking the address of the the return value of
std::use_facet, is not UB. Am I not taking the address of an
r-value in that line?
There might be a problem with using mem_fun with a function that
takes a reference. I haven't tried this particular case, but
I've had problems with it in the past.
Comeau C++ does not complain about it, in any case.
Not all compilers are as good as Comeau.
If that is good, then the following form will aslo work
nicely:
template< typename charT, typename Traits >
std::basic_string< charT, Traits >
toLower(
std::basic_string< charT, Traits > const&
in,
std::locale loc = std::locale() )
{
std::basic_string<charT, Traits> result(in);
std::transform(in.begin(),in.end(),result.begin(),
std::bind1st(std::mem_fun(&std::ctype<charT>::tolower),
&std::use_facet<std::ctype<charT> >(loc)));
return result;
}
Maybe. I can't read it, which means that I won't use it.