c++ - std::basic_regex<char32_t>,有人已经尝试了吗?

标签 c++ regex gcc c++11

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

最佳答案

根据 this reference

Two specializations of std::regex_traits are defined by the standard library:

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

These specializations make it possible to use std::basic_regex<char> (aka std::regex) and std::basic_regex<wchar_t> (aka std::wregex), but in order to use, for example, std::basic_regex<char32_t>, user-provided specializtion std::regex_traits<char32_t> needs to be defined.

std::regex_traits<char32_t>标准没有提供。您必须自己实现才能使用 std::basic_regex<char32_t> .

关于c++ - std::basic_regex<char32_t>,有人已经尝试了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178069/

相关文章:

java - 最多一个字符的正则表达式匹配字符串

ruby - 正则表达式 : how to get every group of MatchData?

c++ - 在 gcc 上没有警告的情况下确定类方法的返回类型

c++ - GCC 中的 unordered_map 错误

c++ - 在循环中执行移位操作时 C++ 中的内存泄漏

c++ - 在写入之前检查 boost::interprocess::vector 是否有足够的容量?

python - 正则表达式获取括号外的所有文本

c++ - : operator in c/c++

c - gcc:从硬件寄存器读取时 '-fno-strict-aliasing' 的奇怪行为

gcc: 无法识别的选项 `-nolibc`