У меня проблемы с функцией форматирования в boost::regex_replace
. Я могу назвать его версию с одним параметром, но не с двумя параметрами:
e = "(^|>)([^<>]+)";
h_str = regex_replace(h_str, e, repl_quot, boost::match_default);
Где repl_quot
определяется как
std::string const &repl_quot(boost::smatch const &what) {
boost::regex e("\"");
std::string repl(""");
static std::string;
str = regex_replace(what[0].str(), e, repl, boost::match_default);
return str;
}
Вышеупомянутое работает, но я действительно не хочу использовать эту статическую переменную, поэтому я попробовал то, что считал приемлемой альтернативной версией с двумя параметрами:
std::string const &repl_quot2(boost::smatch const &what, std::string &out) {
boost::regex e("\"");
std::string repl(""");
out = regex_replace(what[0].str(), e, repl, boost::match_default);
return out;
}
Но regex_replace этого не примет (запутанная ошибка компилятора). Я пытаюсь использовать версию с двумя параметрами на основе следующего из документации Boost::Regex
:
шаблон basic_string regex_replace (const basic_string & s, const basic_regex & e, Formatter fmt, match_flag_type flags = match_default);
Требуется Тип Formatter должен быть либо ... унарным, двоичным или тройным функтором, который вычисляет заменяющую строку из вызова функции: либо fmt (what), который должен возвращать контейнер char_type, который будет использоваться в качестве текста замены, либо либо fmt (what, out) или fmt (what, out, flags), оба из которых записывают текст замены в * out, а затем возвращают новую позицию OutputIterator. В каждом случае какой объект match_results представляет найденное совпадение.
Были неоднократные запросы на сообщение об ошибке компилятора, так что вот оно (будьте осторожны с тем, что вы просите):
c: \ boost \ boost \ regex \ v4 \ regex_format.hpp В функции-члене `OutputIter boost :: re_detail :: format_functor_container :: operator () (const Match &, OutputIter, boost :: regex_constants :: match_flag_type, const Traits &) [с OutputIter = boost :: re_detail :: string_out_iterator, std :: allocator>>, Container = const std :: string & (*) (const boost :: smatch &, std :: string &), Match = boost :: match_results ‹__ gnu_cxx :: __normal_iterator, std :: allocator>>, std :: allocator, std :: allocator>>>>>, Traits = boost :: regex_traits_wrapper>>] ':
356 c: \ boost \ boost \ regex \ v4 \ match_results.hpp, созданный из `OutputIterator boost :: match_results :: format (OutputIterator, Functor, boost :: regex_constants :: match_flag_type, const RegexT &) const [с OutputIterator = boost :: re_detail :: string_out_iterator, std :: allocator>>, Functor = const std :: string & (*) (const boost :: smatch &, std :: string &), RegexT = boost :: basic_regex>>, BidiIterator = __gnu_cxx :: __ normal_iterator , std :: allocator>>, Allocator = std :: allocator, std :: allocator>>>>] '
60 c: \ boost \ boost \ regex \ v4 \ regex_replace.hpp, созданный из `OutputIterator boost :: regex_replace (OutputIterator, BidirectionalIterator, BidirectionalIterator, const boost :: basic_regex &, Formatter, boost :: regex_constants :: match_flag_tetype = boost :: re_detail :: string_out_iterator, std :: allocator>>, BidirectionalIterator = __gnu_cxx :: __ normal_iterator, std :: allocator>>, traits = boost :: regex_traits>, charT = char, Formatter = const std :: string & (* ) (const boost :: smatch &, std :: string &)] '
80 c: \ boost \ boost \ regex \ v4 \ regex_replace.hpp, созданный из `std :: basic_string, std :: allocator ‹_T2>> boost :: regex_replace (const std :: basic_string, std :: allocator ‹_T2>> &, const boost :: basic_regex &, Formatter, boost :: regex_constants :: match_flag_type) [с traits = boost :: regex_traits>, charT = char, Formatter = const std :: string & (*) (const boost :: smatch &, std ::нить&)]'
327 C: \ Dev-Cpp \ Примеры \ wordrad \ xhtml_open.cpp, созданный отсюда
1064 c: \ boost \ boost \ regex \ v4 \ regex_format.hpp запрос для члена _7 _ ((boost :: re_detail :: format_functor_container, std :: allocator>>, std :: allocator, std :: allocator>>>>> , boost :: regex_traits_wrapper>>> *) this) -> boost :: re_detail :: format_functor_container, std :: allocator>>, std :: allocator, std :: allocator>>>>>, boost :: regex_traits_wrapper>> > :: func ', который имеет неклассовый тип `const std :: string & (* const) (const boost :: smatch &, std :: string &)'
1064 c: \ boost \ boost \ regex \ v4 \ regex_format.hpp запрос для члена _8 _ ((boost :: re_detail :: format_functor_container, std :: allocator>>, std :: allocator, std :: allocator>>>>> , boost :: regex_traits_wrapper>>> *) this) -> boost :: re_detail :: format_functor_container, std :: allocator>>, std :: allocator, std :: allocator>>>>>, boost :: regex_traits_wrapper>> > :: func ', который имеет неклассовый тип `const std :: string & (* const) (const boost :: smatch &, std :: string &)'
static std::string;
чего-то не хватает, либо точку с запятой там нужно убрать. И я не думаю, чтоstr
должен быть статичным в этом примере. Можете ли вы опубликовать все сообщение об ошибке в вопросе, так как этих комментариев недостаточно для полного сообщения об ошибке. - person wimh   schedule 30.01.2011