【问题标题】:Clang 3.5.0 std::wregex compile errorClang 3.5.0 std::wregex 编译错误
【发布时间】:2014-12-21 16:06:55
【问题描述】:

当我尝试创建宽正则表达式 (std::wregex) 时,Clang 打印出一个奇怪的错误

#include <regex>
#include <string>

int main() {
    std::wregex myRegex( L"Hello Regex" );
    return 0;
}

此代码使用 g++ (--std=c++11 -Wall) 可以正常编译,没有任何错误或警告,但 clang++ (--std=c++11) 会生成此错误:

In file included from test.cpp:1:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/regex:60:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:20: error: non-type template argument is not a constant expression
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:398:53: note: in instantiation of template class
      'std::__detail::_BracketMatcher<std::regex_traits<wchar_t>, false, false>' requested here
      _BracketMatcher<_TraitsT, __icase, __collate> __matcher
                                                    ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:316:25: note: in instantiation of function template specialization
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_insert_character_class_matcher<false, false>' requested here
        __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
                               ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:288:8: note: expanded from macro '__INSERT_REGEX_MATCHER'
              __func<false, false>(args);\
              ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:136:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_atom' requested here
      if (this->_M_atom())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:118:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_term' requested here
      if (this->_M_term())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:97:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_alternative' requested here
      this->_M_alternative();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:82:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_disjunction' requested here
      this->_M_disjunction();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:155:14: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_Compiler' requested here
      return _Cmplr(__first, __last, __traits, __flags)._M_get_nfa();
             ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:532:27: note: in instantiation of function template specialization
      'std::__detail::__compile_nfa<std::regex_traits<wchar_t> >' requested here
          _M_automaton(__detail::__compile_nfa(_M_original_str.c_str(),
                                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:452:9: note: in instantiation of function template specialization 'std::basic_regex<wchar_t,
      std::regex_traits<wchar_t> >::basic_regex<const wchar_t *>' requested here
      : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
        ^
test.cpp:5:17: note: in instantiation of member function 'std::basic_regex<wchar_t, std::regex_traits<wchar_t> >::basic_regex' requested here
    std::wregex myRegex( L"Hello Regex" );
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:22: note: shift count 32 >= width of type 'int' (32 bits)
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:20: error: non-type template argument is not a constant expression
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:398:53: note: in instantiation of template class
      'std::__detail::_BracketMatcher<std::regex_traits<wchar_t>, false, true>' requested here
      _BracketMatcher<_TraitsT, __icase, __collate> __matcher
                                                    ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:316:25: note: in instantiation of function template specialization
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_insert_character_class_matcher<false, true>' requested here
        __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
                               ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:290:8: note: expanded from macro '__INSERT_REGEX_MATCHER'
              __func<false, true>(args);\
              ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:136:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_atom' requested here
      if (this->_M_atom())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:118:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_term' requested here
      if (this->_M_term())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:97:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_alternative' requested here
      this->_M_alternative();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:82:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_disjunction' requested here
      this->_M_disjunction();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:155:14: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_Compiler' requested here
      return _Cmplr(__first, __last, __traits, __flags)._M_get_nfa();
             ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:532:27: note: in instantiation of function template specialization
      'std::__detail::__compile_nfa<std::regex_traits<wchar_t> >' requested here
          _M_automaton(__detail::__compile_nfa(_M_original_str.c_str(),
                                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:452:9: note: in instantiation of function template specialization 'std::basic_regex<wchar_t,
      std::regex_traits<wchar_t> >::basic_regex<const wchar_t *>' requested here
      : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
        ^
test.cpp:5:17: note: in instantiation of member function 'std::basic_regex<wchar_t, std::regex_traits<wchar_t> >::basic_regex' requested here
    std::wregex myRegex( L"Hello Regex" );
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:22: note: shift count 32 >= width of type 'int' (32 bits)
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:20: error: non-type template argument is not a constant expression
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:398:53: note: in instantiation of template class
      'std::__detail::_BracketMatcher<std::regex_traits<wchar_t>, true, false>' requested here
      _BracketMatcher<_TraitsT, __icase, __collate> __matcher
                                                    ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:316:25: note: in instantiation of function template specialization
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_insert_character_class_matcher<true, false>' requested here
        __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
                               ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:293:8: note: expanded from macro '__INSERT_REGEX_MATCHER'
              __func<true, false>(args);\
              ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:136:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_atom' requested here
      if (this->_M_atom())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:118:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_term' requested here
      if (this->_M_term())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:97:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_alternative' requested here
      this->_M_alternative();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:82:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_disjunction' requested here
      this->_M_disjunction();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:155:14: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_Compiler' requested here
      return _Cmplr(__first, __last, __traits, __flags)._M_get_nfa();
             ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:532:27: note: in instantiation of function template specialization
      'std::__detail::__compile_nfa<std::regex_traits<wchar_t> >' requested here
          _M_automaton(__detail::__compile_nfa(_M_original_str.c_str(),
                                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:452:9: note: in instantiation of function template specialization 'std::basic_regex<wchar_t,
      std::regex_traits<wchar_t> >::basic_regex<const wchar_t *>' requested here
      : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
        ^
test.cpp:5:17: note: in instantiation of member function 'std::basic_regex<wchar_t, std::regex_traits<wchar_t> >::basic_regex' requested here
    std::wregex myRegex( L"Hello Regex" );
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:22: note: shift count 32 >= width of type 'int' (32 bits)
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:20: error: non-type template argument is not a constant expression
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:398:53: note: in instantiation of template class
      'std::__detail::_BracketMatcher<std::regex_traits<wchar_t>, true, true>' requested here
      _BracketMatcher<_TraitsT, __icase, __collate> __matcher
                                                    ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:316:25: note: in instantiation of function template specialization
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_insert_character_class_matcher<true, true>' requested here
        __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
                               ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:295:8: note: expanded from macro '__INSERT_REGEX_MATCHER'
              __func<true, true>(args);\
              ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:136:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_atom' requested here
      if (this->_M_atom())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:118:17: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_term' requested here
      if (this->_M_term())
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:97:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_alternative' requested here
      this->_M_alternative();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.tcc:82:13: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_M_disjunction' requested here
      this->_M_disjunction();
            ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:155:14: note: in instantiation of member function
      'std::__detail::_Compiler<std::regex_traits<wchar_t> >::_Compiler' requested here
      return _Cmplr(__first, __last, __traits, __flags)._M_get_nfa();
             ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:532:27: note: in instantiation of function template specialization
      'std::__detail::__compile_nfa<std::regex_traits<wchar_t> >' requested here
          _M_automaton(__detail::__compile_nfa(_M_original_str.c_str(),
                                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex.h:452:9: note: in instantiation of function template specialization 'std::basic_regex<wchar_t,
      std::regex_traits<wchar_t> >::basic_regex<const wchar_t *>' requested here
      : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
        ^
test.cpp:5:17: note: in instantiation of member function 'std::basic_regex<wchar_t, std::regex_traits<wchar_t> >::basic_regex' requested here
    std::wregex myRegex( L"Hello Regex" );
                ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/regex_compiler.h:413:22: note: shift count 32 >= width of type 'int' (32 bits)
                                   std::bitset<1 << (8 * sizeof(_CharT))>,
                                                 ^
4 errors generated.

GCC 版本:4.9.2

叮当声:

clang version 3.5.0 (tags/RELEASE_350/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix

这是一个 clang 错误还是 clang 需要一些额外的标题?普通的 std::regex 可与 gcc 和 clang 一起使用。

【问题讨论】:

    标签: c++ regex c++11 clang++


    【解决方案1】:

    它已在 libstdc++ 主干中修复,但未向后移植到 4.9。

    https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc++-v3/include/bits/regex_compiler.h?r1=211143&r2=211142&pathrev=211143

    https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc++-v3/include/bits/regex_compiler.h?r1=218322&r2=218321&pathrev=218322

    这是因为 1 &lt;&lt; (8 * sizeof(_CharT))1 &lt;&lt; 32(_CharT 是 wchar_t,这里是 4 个字节大)并且它会导致未定义的行为,因为这里 1 的类型为 intsizeof(int) 是 4。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 2013-08-20
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 2015-12-25
      • 2017-06-27
      相关资源
      最近更新 更多