【问题标题】:std::basic_regex<char32_t>, Is someone already try it?std::basic_regex<char32_t>,有人已经尝试了吗?
【发布时间】:2014-11-28 11:02:40
【问题描述】:

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

【问题讨论】:

    标签: c++ regex gcc c++11


    【解决方案1】:

    根据this reference

    标准库定义了std::regex_traits 的两个特化:

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

    这些特化使得使用std::basic_regex&lt;char&gt;(又名std::regex)和std::basic_regex&lt;wchar_t&gt;(又名std::wregex)成为可能,但是为了使用例如std::basic_regex&lt;char32_t&gt;,用户提供的特化std::regex_traits&lt;char32_t&gt;需要定义。

    std::regex_traits&lt;char32_t&gt; 不是标准提供的。您必须自己实现它才能使用std::basic_regex&lt;char32_t&gt;

    【讨论】:

    • 感谢您的快速回答,我可以理解需要指定 std::regex_traits 但我不明白 clang 的构建错误。
    • 在尝试创建 std::regex_traits 之后,创建它可能真的很复杂 ==> 然后我将使用非常有效的原生 utf8 字符串。 (我希望 UTF32 支持很快就会出现在 STD 中)
    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2010-09-20
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多