Christopher Smith on 16 Aug 2003 02:47:57 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] compact, insane example code


On Fri, 2003-08-15 at 17:49, Christopher Smith wrote:
> Ugh! And I was going to say he should use locales/facets for his
> uppercase() function. Sadly, those suckers *only* work with c-style
> strings. :-(
> 
> <wishing STL didn't treat it's own strings as 2nd class citizens>

So, I thought I'd managed to come up with a solution that made me
reasonably happy, but was still fairly complex (;-):

const std::locale DEFAULT_LOCALE("");

inline string& uppercase(string& input, const std::locale& loc =
DEFAULT_LOCALE) 
{
    const std::ctype<char>& ct = std::use_facet< std::ctype<char>
>(loc);
    transform(input.begin(), input.end(), 
	      input.begin(), 
	      bind<char>(&std::ctype<char>::toupper, ct, _1));
    return input;
}

inline string uppercase(const string& input, const std::locale& loc =
DEFAULT_LOCALE)
{
    string result(input);
    uppercase(result, loc);
    return result;
}

Interestingly though, the bind is failing with g++. It appears as though
the problem is coming from some confusion in the compiler as to which
member function I'm trying to bind to. There are two member functions to
choose from for toupper:

char_type toupper(char_type) const;
const char_type* toupper(char_type*, const char_type*) const;

For some reason, no matter what I try, it is determined to use the
second. Am I missing something (yeah, like a real understanding of
boost::lambda ;-), or is g++ just being difficult? The latter just seems
totally wrong.

This all STL solution works, but demonstrates how nice it would be to
have a decent bind function like that found in lambda:

inline string& uppercase(string& input, const std::locale& loc =
DEFAULT_LOCALE) 
{
    const std::ctype<char>& ct = std::use_facet< std::ctype<char>
>(loc);
    std::const_mem_fun1_t<char, std::ctype<char>, char>
temp(&std::ctype<char>::toupper);
    std::transform(input.begin(), input.end(), 
		   input.begin(), 
		   std::bind1st(temp, &ct));
    return input;
}

inline string uppercase(const string& input, const std::locale& loc =
DEFAULT_LOCALE)
{
    string result(input);
    uppercase(result, loc);
    return result;
}

--Chris
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp