我认为当您定义BOOST_REGEX_MATCH_EXTRA 时,实现中存在错误。对我来说,它确实可以编译(同时使用 boost 1.64 和 1.66),但是,它在运行时因 UBSAN 诊断而失败(意味着存在未定义的行为)。诊断是,例如:
/home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426:36: runtime error: member call on misaligned address 0x000000000001 for type 'struct sp_counted_base', which requires 8 byte alignment
0x000000000001: note: pointer points here
<memory cannot be printed>
/home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426:36: runtime error: member access within misaligned address 0x000000000001 for type 'struct sp_counted_base', which requires 8 byte alignment
0x000000000001: note: pointer points here
<memory cannot be printed>
ASAN:DEADLYSIGNAL
=================================================================
==24391==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000001 (pc 0x000000406f73 bp 0x7ffe19c54a80 sp 0x7ffe19c54a60 T0)
==24391==The signal is caused by a READ memory access.
==24391==Hint: address points to the zero page.
#0 0x406f72 in boost::detail::shared_count::~shared_count() /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426
#1 0x407800 in boost::shared_ptr<boost::re_detail_106600::named_subexpressions>::~shared_ptr() /home/sehe/custom/boost_1_66_0/boost/smart_ptr/shared_ptr.hpp:341
#2 0x407a26 in boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::~match_results() /home/sehe/custom/boost_1_66_0/boost/regex/v4/match_results.hpp:110
#3 0x404c17 in print_captures(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/sehe/Projects/stackoverflow/test.cpp:7
#4 0x404f81 in main /home/sehe/Projects/stackoverflow/test.cpp:37
#5 0x7f70051e482f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#6 0x404718 in _start (/home/sehe/Projects/stackoverflow/sotest+0x404718)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426 in boost::detail::shared_count::~shared_count()
==24391==ABORTING
事实上,编译器定义BOOST_REGEX_MATCH_EXTRA 只需要额外的捕获信息(第一个链接示例)。如果您不需要它,只需在没有定义的情况下进行编译,所有其他东西都应该可以工作。
Live On Coliru
#include <boost/regex.hpp>
#include <iostream>
void print_captures(const std::string ®x, const std::string &text) {
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if (boost::regex_match(text, what, e, boost::match_extra)) {
std::cout << "** Match found **\n Sub-Expressions:\n";
for (unsigned i = 0; i < what.size(); ++i)
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
#ifdef BOOST_REGEX_MATCH_EXTRA
std::cout << " Captures:\n";
for (unsigned i = 0; i < what.size(); ++i) {
std::cout << " $" << i << " = {";
for (unsigned j = 0; j < what.captures(i).size(); ++j) {
if (j)
std::cout << ", ";
else
std::cout << " ";
std::cout << "\"" << what.captures(i)[j] << "\"";
}
std::cout << " }\n";
}
#endif
} else {
std::cout << "** No Match found **\n";
}
}
int main(int, char *[]) {
print_captures(R"((([[:lower:]]+)|([[:upper:]]+))+)", "aBBcccDDDDDeeeeeeee");
print_captures(R"((.*)bar|(.*)bah)", "abcbar");
print_captures(R"((.*)bar|(.*)bah)", "abcbah");
print_captures(R"(^(?:(\w+)|(?>\W+))*$)", "now is the time for all good men to come to the aid of the party");
}
打印
Expression: "(([[:lower:]]+)|([[:upper:]]+))+"
Text: "aBBcccDDDDDeeeeeeee"
** Match found **
Sub-Expressions:
$0 = "aBBcccDDDDDeeeeeeee"
$1 = "eeeeeeee"
$2 = "eeeeeeee"
$3 = "DDDDD"
Expression: "(.*)bar|(.*)bah"
Text: "abcbar"
** Match found **
Sub-Expressions:
$0 = "abcbar"
$1 = "abc"
$2 = ""
Expression: "(.*)bar|(.*)bah"
Text: "abcbah"
** Match found **
Sub-Expressions:
$0 = "abcbah"
$1 = ""
$2 = "abc"
Expression: "^(?:(\w+)|(?>\W+))*$"
Text: "now is the time for all good men to come to the aid of the party"
** Match found **
Sub-Expressions:
$0 = "now is the time for all good men to come to the aid of the party"
$1 = "party"