Iteratiing on regex results with pointer-to-member-function
Hi, I wrote some code where I want to iterate on the result of a regex matching and call a member function for each match. It goes roughly like this: ====================================================================== class XX { typedef bool (XX::*FuncPtr)(const boost::match_resultsstd::string::const_iterator& what); FuncPtr funcPtr; }; bool XX::regex_callback(const boost::match_resultsstd::string::const_iterator& what) { return true; } void XX::addDelayedVariables(string expr) { funcPtr = &XX::regex_callback; regex re("([[:alpha::]][[:alnum]]+)(\[[^\\]]+])"); sregex_iterator m1(expr.begin(), expr.end(), re); sregex_iterator m2; match_resultsstring::const_iterator results; (*) bool a = (this->*funcPtr)(results); (*) for_each(m1, m2, (this->*funcPtr)); (#) } =========================================================================== When compiling (gcc 4.1 on FC 6) I get an error message at (#) that says: invalid use of non-static member function I added the two lines with a (*) as a sanity check to see that I declared the pointer-to-member-function correctly, and I have no problem compiling those. So my question is: is this a problem with me not understanding how to handle pointer-to-member-functions in C++, or is there something with the boost libraries that I'm missing or is preventing me from performing this operation? I would appreciate any help or opinion. thanks, Alain
Alain Leblanc wrote:
Hi,
I wrote some code where I want to iterate on the result of a regex matching and call a member function for each match. It goes roughly like this:
void XX::addDelayedVariables(string expr) { funcPtr = &XX::regex_callback; regex re("([[:alpha::]][[:alnum]]+)(\[[^\\]]+])"); sregex_iterator m1(expr.begin(), expr.end(), re); sregex_iterator m2; match_resultsstring::const_iterator results; (*) bool a = (this->*funcPtr)(results); (*) for_each(m1, m2, (this->*funcPtr)); (#) } ===========================================================================
When compiling (gcc 4.1 on FC 6) I get an error message at (#) that says: invalid use of non-static member function
I added the two lines with a (*) as a sanity check to see that I declared the pointer-to-member-function correctly, and I have no problem compiling those.
So my question is: is this a problem with me not understanding how to handle pointer-to-member-functions in C++, or is there something with the boost libraries that I'm missing or is preventing me from performing this operation? I would appreciate any help or opinion.
The problem is that (this->*funcPtr) is an unmentionable and unusable type in C++. Yes, you can use it at the site of a function call, but not as a functor. I'm sure Boost.Bind or Boost.Lambda have solutions, or you can use: std::bind1st(std::mem_fun(&XX::regex_callback), this) to create a valid functor to pass to for_each. HTH, John.
Thanks for the reply. I'll give it a try when I have some time. For
now I found an easy way around the problem.
a
2007/6/30, John Maddock
Alain Leblanc wrote:
Hi,
I wrote some code where I want to iterate on the result of a regex matching and call a member function for each match. It goes roughly like this:
void XX::addDelayedVariables(string expr) { funcPtr = &XX::regex_callback; regex re("([[:alpha::]][[:alnum]]+)(\[[^\\]]+])"); sregex_iterator m1(expr.begin(), expr.end(), re); sregex_iterator m2; match_resultsstring::const_iterator results; (*) bool a = (this->*funcPtr)(results); (*) for_each(m1, m2, (this->*funcPtr)); (#) } ===========================================================================
When compiling (gcc 4.1 on FC 6) I get an error message at (#) that says: invalid use of non-static member function
I added the two lines with a (*) as a sanity check to see that I declared the pointer-to-member-function correctly, and I have no problem compiling those.
So my question is: is this a problem with me not understanding how to handle pointer-to-member-functions in C++, or is there something with the boost libraries that I'm missing or is preventing me from performing this operation? I would appreciate any help or opinion.
The problem is that (this->*funcPtr) is an unmentionable and unusable type in C++. Yes, you can use it at the site of a function call, but not as a functor.
I'm sure Boost.Bind or Boost.Lambda have solutions, or you can use:
std::bind1st(std::mem_fun(&XX::regex_callback), this)
to create a valid functor to pass to for_each.
HTH, John.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Alain Leblanc
-
John Maddock