2

Well how do I invoke a custom formatting function when calling boost::regex_replace?

My code is as following:

template <typename T>
std::string fmt(boost::match_results<T> match) {
    auto str = match[1];
    if (str == ".") {
        return "\".\"";
    } else {
        return str;
    }
}
void __ConvertEscapeChar(std::string& action, std::string regex) {
    boost::regex re(regex);
    action = boost::regex_replace(action, re, &fmt, boost::regex_constants::match_all);
}

however it shows an error, "could not deduce template argument for __fmt". - Well what IS T actually?

paul23
  • 8,799
  • 12
  • 66
  • 149
  • 1
    Your function names are invalid. See [here](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Benjamin Lindley Jan 26 '12 at 20:16
  • @BenjaminLindley Meh changed... But that dosen't really change anything.. (really need to a new PREfix to identify local functions now). – paul23 Jan 26 '12 at 20:22
  • 1
    It changes your code to valid C++, other than your current error. – Benjamin Lindley Jan 26 '12 at 20:28
  • If by "local functions" you mean a function that shouldn't be visible outside the current compilation unit (.cc/.o file), you can put it in an anonymous namespace: `namespace { ConvertEscapeChar(...) {...} }`. – Tim Ruddick Aug 23 '12 at 20:41

1 Answers1

0

Unless you need the flexibility of using a template in your fmt function for some reason that isn't obvious here, try this instead:

std::string fmt(boost::smatch match)
Tim Ruddick
  • 1,375
  • 16
  • 24