
Hi, I'm trying to find the first non-space character of a string using boost::bind, but I simply can't get this to compile: --- static void Trim(std::string& stringToTrim) { std::string::iterator firstNonSpace = std::find_if(stringToTrim.begin(), stringToTrim.end(), boost::bind(std::not_equal_to<int>(), boost::bind(&isspace, _1), 0)); .... --- Am I doing something seriously wrong, or is this a compiler limitation (VC.NET)? (If anyone has a method to do this without using isspace, I'd be happy to see it). Thanks // Johan

Sorry, typo; not_equal_to should be equal_to. // Johan "judoka1981" <johan.nilsson@esrange.ssc.se> wrote in message news:ammnpc+7vpk@eGroups.com...
Hi,
I'm trying to find the first non-space character of a string using boost::bind, but I simply can't get this to compile:
---
static void Trim(std::string& stringToTrim) { std::string::iterator firstNonSpace = std::find_if(stringToTrim.begin(), stringToTrim.end(), boost::bind(std::not_equal_to<int>(), boost::bind(&isspace, _1), 0));
....
---
Am I doing something seriously wrong, or is this a compiler limitation (VC.NET)?
(If anyone has a method to do this without using isspace, I'd be happy to see it).
Thanks // Johan

From: "judoka1981" <johan.nilsson@esrange.ssc.se>
Hi,
I'm trying to find the first non-space character of a string using boost::bind, but I simply can't get this to compile:
---
static void Trim(std::string& stringToTrim) { std::string::iterator firstNonSpace = std::find_if(stringToTrim.begin(), stringToTrim.end(), boost::bind(std::not_equal_to<int>(), boost::bind(&isspace, _1), 0));
This may or may not compile, depending on whether <locale> is included (it contains a two-argument std::isspace), and depending on which isspace's are in the global namespace (<ctype.h> vs <cctype>). One possible fix is to use int (*isspace)(int) = &std::isspace; // use the <cctype> isspace but using isspace(ch) in this way isn't portable as char may be signed, the portable use is isspace(static_cast<unsigned char>(ch)). The other approach is bool (*isspace)(char, std::locale const &) = &std::isspace; // use the <locale> isspace and change the inner bind to boost::bind(isspace, _1, l) where l is the locale to use. Also, you may consider using std::logical_not<bool> in the outer bind; on MSVC.NET, due to lack of function template partial ordering, you'll need to tell bind the return type, too: bind<bool>.

Thanks for the reply, I tried the following: --- std::locale defaultLocale; bool (*pIsSpace)(char, const std::locale&) = &std::isspace; std::string::iterator nonSpace = std::find_if(stringToTrim.begin(), stringToTrim.end(), boost::bind<bool>(std::logical_not<bool>(), boost::bind<bool>(pIsSpace, _1, defaultLocale))); --- But only got "error C2667: 'boost::bind' : none of 38 overloads have a best conversion" etc ... I'm restricted to boost 1.27.0 in the project, could this cause any problems? // Johan

From: "Johan Nilsson" <johan.nilsson@esrange.ssc.se>
Thanks for the reply,
I tried the following:
---
std::locale defaultLocale; bool (*pIsSpace)(char, const std::locale&) = &std::isspace;
std::string::iterator nonSpace = std::find_if(stringToTrim.begin(), stringToTrim.end(), boost::bind<bool>(std::logical_not<bool>(), boost::bind<bool>(pIsSpace, _1, defaultLocale)));
---
But only got "error C2667: 'boost::bind' : none of 38 overloads have a best conversion" etc ...
You only need bind<bool> on the outer bind, as it uses a function object. The inner bind uses a function pointer, and there is no need to pass a type. http://www.boost.org/libs/bind/bind.html#Q_forms http://www.boost.org/libs/bind/bind.html#err_short_form http://www.boost.org/libs/bind/bind.html#err_long_form On a more capable compiler, even the outer bind doesn't need the <bool> as it will automatically extract the return type from the ::result_type nested typedef, but MSVC 7 can't do that.
participants (3)
-
Johan Nilsson
-
judoka1981
-
Peter Dimov