Станд :: basic_regex , Кто-то уже пытается это сделать?

2:

#include <iostream>
#include <string>
#include <regex>
int main() { std::u32string lines[] = {U"Roses are #ff0000", U"violets are #0000ff", U"all of my base are belong to you"}; std::basic_regex<char32_t> color_regex(U"#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})"); /* for (const auto &line : lines) { std::cout << line << ": " << std::regex_search(line, color_regex) << '\n'; } std::smatch color_match; for (const auto &line : lines) { std::regex_search(line, color_match, color_regex); std::cout << "matches for '" << line << "'\n"; for (size_t i = 0; i < color_match.size(); ++i) { std::ssub_match sub_match = color_match[i]; std::string sub_match_str = sub_match.str(); std::cout << i << ": " << sub_match_str << '\n'; } } */ return 0;
}
</char32_t></regex></string></iostream>

скомпилировать с gcc: он работает:

g++ -g -O0 -std=c++11 regexp.cpp -o executable

компилировать с clang: он вообще не работает:

clang++ -g -O0 -std=c++11 regexp.cpp -o executable

выполнить с gcc сгенерированным файлом:

terminate called after throwing an instance of 'std::bad_cast' what(): std::bad_cast
Abandon (core dumped)

ошибка в компиляции clang:

In file included from regexp.cpp:3:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/regex:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.h:413:20: error: non-type template argument is not a constant expression std::bitset<1 << (8 * sizeof(_CharT))>, ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.tcc:398:53: note: in instantiation of template class 'std::__detail::_BracketMatcher<std::regex_traits<char32_t>, false, false>' requested here _BracketMatcher<_TraitsT, __icase, __collate> __matcher ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.tcc:316:25: note: in instantiation of function template specialization 'std::__detail::_Compiler<std::regex_traits<char32_t> >::_M_insert_character_class_matcher<false, false="">' requested here __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher); ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.tcc:288:8: note: expanded from macro '__INSERT_REGEX_MATCHER' __func<false, false="">(args);\ ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.tcc:136:17: note: in instantiation of member function 'std::__detail::_Compiler<std::regex_traits<char32_t> >::_M_atom' requested here if (this->_M_atom()) ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.tcc:118:17: note: in instantiation of member function 'std::__detail::_Compiler<std::regex_traits<char32_t> >::_M_term' requested here if (this->_M_term()) ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.tcc:97:13: note: in instantiation of member function 'std::__detail::_Compiler<std::regex_traits<char32_t> >::_M_alternative' requested here this->_M_alternative(); ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.tcc:82:13: note: in instantiation of member function 'std::__detail::_Compiler<std::regex_traits<char32_t> >::_M_disjunction' requested here this->_M_disjunction(); ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.h:155:14: note: in instantiation of member function 'std::__detail::_Compiler<std::regex_traits<char32_t> >::_Compiler' requested here return _Cmplr(__first, __last, __traits, __flags)._M_get_nfa(); ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex.h:524:27: note: in instantiation of function template specialization 'std::__detail::__compile_nfa<std::regex_traits<char32_t> >' requested here _M_automaton(__detail::__compile_nfa(_M_original_str.c_str(), ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex.h:452:9: note: in instantiation of function template specialization 'std::basic_regex<char32_t, std::regex_traits<char32_t=""> >::basic_regex<const char32_t="" *="">' requested here : basic_regex(__p, __p + _Rx_traits::length(__p), __f) ^
regexp.cpp:10:29: note: in instantiation of member function 'std::basic_regex<char32_t, std::regex_traits<char32_t=""> >::basic_regex' requested here std::basic_regex<char32_t> color_regex(U"#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})"); ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/regex_compiler.h:413:22: note: shift count 32 >= width of type 'int' (32 bits) std::bitset<1 << (8 * sizeof(_CharT))>, ^
...
...
...
...
</char32_t></char32_t,></const></char32_t,></std::regex_traits<char32_t></std::regex_traits<char32_t></std::regex_traits<char32_t></std::regex_traits<char32_t></std::regex_traits<char32_t></std::regex_traits<char32_t></false,></false,></std::regex_traits<char32_t></std::regex_traits<char32_t>

Попросите кого-нибудь попробовать?

$ clang --version
Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ g++ --version
g++ (Ubuntu 4.9.1-3ubuntu2~14.04.1) 4.9.1
1 ответ

Согласно этой ссылке

Две специализации <code>std::regex_traits</code> определяются стандартной библиотекой:

std::regex_traits<char>
std::regex_traits</char>

Эти специализации позволяют использовать <code>std::basic_regex</code> (aka <code>std::regex</code>) и <code>std::basic_regex</code> (aka <code>std::wregex</code>), но для использования, например, <code>std::basic_regex</code>, необходимо <code>std::regex_traits</code> предоставленную пользователем <code>std::regex_traits</code>.

std::regex_traits не предоставляется стандартом. Вы должны реализовать его самостоятельно, чтобы использовать std::basic_regex.

licensed under cc by-sa 3.0 with attribution.