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;
}
用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))>,
^
...
...
...
...
有人已经尝试过吗?
$ 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
答案 0 :(得分:2)
std::regex_traits
的两个特化由标准库定义:
std::regex_traits<char>
std::regex_traits<wchar_t>
标准不提供这些专业化可以使用
std::basic_regex<char>
(又名std::regex
)和std::basic_regex<wchar_t>
(又名std::wregex
),但为了使用,例如,{{1需要定义用户提供的特殊标识std::basic_regex<char32_t>
。
std::regex_traits<char32_t>
。您必须自己实施才能使用std::regex_traits<char32_t>
。