繁体   English   中英

的std :: basic_regex <char32_t> ,是否已经有人尝试过?

[英]std::basic_regex<char32_t>, Is someone already try it?

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)

lang编译错误:

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

根据这个参考

标准库定义了std::regex_traits两个std::regex_traits

std::regex_traits<char>
std::regex_traits<wchar_t>

这些专业知识使得可以使用std::basic_regex<char> (aka std::regex )和std::basic_regex<wchar_t> (aka std::wregex ),但是要使用例如std::basic_regex<char32_t> ,需要定义用户提供的特殊化std::regex_traits<char32_t>

std::regex_traits<char32_t>未提供std::regex_traits<char32_t> 您必须自己实现它才能使用std::basic_regex<char32_t>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM