Re: [Boost-bugs] [Boost C++ Libraries] #4020: regex::match_results::format functor cannot use member variables

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #4020: regex::match_results::format functor cannot use member variables
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-03-18 16:32:43

#4020: regex::match_results::format functor cannot use member variables
  Reporter: Keith MacDonald <keith@…> | Owner: johnmaddock
      Type: Support Requests | Status: closed
 Milestone: Boost 1.43.0 | Component: regex
   Version: Boost 1.42.0 | Severity: Problem
Resolution: fixed | Keywords: regex functor

Comment(by Keith MacDonald <keith@…>):

 Thanks for the clarification. Returning to C++ after a break, I had
 forgotten that it was a standard library convention to pass functors by
 value. Anyway, I've amended my example code to take account of that, and
 you're welcome to use it in the documentation:

 // Code to insert an incremented count before each word in the source

 #include <stdlib.h>
 #include <iostream>
 #include <sstream>
 #include <boost/regex.hpp>
 #include <boost/shared_ptr.hpp>

 typedef wchar_t char_t;
 typedef std::wstring string_t;

 template <class M>
 class functor
     functor() : m_pImpl(new functor_data) {}

     template <class O>
     O operator()(const M& m, O i)
         // Generate the sequence number in brackets:
         char_t buf[16];
         O i2(i);

         *i2++ = L'(';
         _itow(++m_pImpl->nCounter, buf, 10);
         i2 = std::copy(buf, buf + wcslen(buf), i2);
         *i2++ = L')';

         // Copy the matched text;
         return m.format(i2, L"$0");

     struct functor_data
         functor_data() : nCounter(0) {}
         int nCounter;

     boost::shared_ptr<functor_data> m_pImpl;

 int wmain(int argc, char_t* argv[])
     string_t sText(L"The quick brown fox jumped over the lazy dog");
     string_t sRE(L"\\w+");

     boost::regex_constants::match_flag_type matchFlags =

     boost::basic_regex<char_t> e;
     e.assign(sRE, matchFlags);

     typedef string_t::const_iterator string_iterator;
     boost::regex_iterator<string_iterator> reit(sText.begin(),
 sText.end(), e, matchFlags);
     boost::regex_iterator<string_iterator> reEnd;
     boost::match_results<string_iterator> last_m = *reit;
     std::basic_ostringstream<char_t> stream;
     std::ostream_iterator<char_t, char_t> streamIter(stream);

     functor<boost::match_results<string_iterator> > func;

     for ( ; reit != reEnd; ++reit) {
         const boost::match_results<string_iterator>& m = *reit;

         // copy the source up to this match:
         std::copy(m.prefix().first, m.prefix().second, streamIter);

         // replace the matched text:
         m.format(streamIter, func);
         last_m = m;

     // copy the remainder of the source string:
     std::copy(last_m.suffix().first, last_m.suffix().second, streamIter);

     // output the result:
     std::wcout << stream.str() << std::endl;

     return 0;

Ticket URL: <>
Boost C++ Libraries <>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:02 UTC