【问题标题】:Error while trying to compile c++11 regex tutorial with Clang++ on Linux尝试在 Linux 上使用 Clang++ 编译 c++11 正则表达式教程时出错
【发布时间】:2012-09-05 09:01:56
【问题描述】:

我正在尝试关注this tutorial 关于 C++11 中的正则表达式。当我尝试编译这个小代码示例时,我得到了这些错误

clang++ -std=c++0x test.cpp -o test
In file included from test.cpp:3:
In file included from /usr/include/c++/4.6/regex:55:
/usr/include/c++/4.6/bits/regex_constants.h:196:36: error: constexpr variable
      'match_default' must be initialized by a constant expression
  static constexpr match_flag_type match_default     = 0;
                                   ^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:203:36: error: constexpr variable
      'match_not_bol' must be initialized by a constant expression
  static constexpr match_flag_type match_not_bol     = 1 << _S_not_bol;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:210:36: error: constexpr variable
      'match_not_eol' must be initialized by a constant expression
  static constexpr match_flag_type match_not_eol     = 1 << _S_not_eol;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:216:36: error: constexpr variable
      'match_not_bow' must be initialized by a constant expression
  static constexpr match_flag_type match_not_bow     = 1 << _S_not_bow;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:222:36: error: constexpr variable
      'match_not_eow' must be initialized by a constant expression
  static constexpr match_flag_type match_not_eow     = 1 << _S_not_eow;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:228:36: error: constexpr variable
      'match_any' must be initialized by a constant expression
  static constexpr match_flag_type match_any         = 1 << _S_any;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:233:36: error: constexpr variable
      'match_not_null' must be initialized by a constant expression
  static constexpr match_flag_type match_not_null    = 1 << _S_not_null;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:238:36: error: constexpr variable
      'match_continuous' must be initialized by a constant expression
  static constexpr match_flag_type match_continuous  = 1 << _S_continuous;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:245:36: error: constexpr variable
      'match_prev_avail' must be initialized by a constant expression
  static constexpr match_flag_type match_prev_avail  = 1 << _S_prev_avail;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:273:36: error: constexpr variable
      'format_default' must be initialized by a constant expression
  static constexpr match_flag_type format_default    = 0;
                                   ^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:281:36: error: constexpr variable
      'format_sed' must be initialized by a constant expression
  static constexpr match_flag_type format_sed        = 1 << _S_sed;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:288:36: error: constexpr variable
      'format_no_copy' must be initialized by a constant expression
  static constexpr match_flag_type format_no_copy    = 1 << _S_no_copy;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/regex_constants.h:294:36: error: constexpr variable
      'format_first_only' must be initialized by a constant expression
  static constexpr match_flag_type format_first_only = 1 << _S_first_only;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 errors generated.

怎么了?

【问题讨论】:

  • 它不是一个 constexpr 它只是一个 const 你为什么要在那里使用 constexpr?你没有计算任何东西。也就是说,我认为真正的问题是它不认为 1 是正确类型的文字???因为您没有计算值,所以只需将其更改为 const?
  • match_default = 0; 在我看来确实是一个常量表达式。我想知道这是否只是您的 Clang 版本中的一个错误?您使用的是什么版本的 Clang 和 GCC(我假设它提供了您的 libstdc++)?
  • 尝试使用 libc++ 而不是 libstdc++。
  • 没有肯尼。它给了我同样的错误。我开始认为这个版本的clang/libstdc++/Ubuntu存在一些问题。我会尝试另一种配置...
  • libstdc++ 对 c++11 正则表达式的支持目前已被彻底破坏。有关详细信息,请参阅gcc.gnu.org/onlinedocs/libstdc++/manual/…。即使你得到了要编译的代码,即使是最简单的正则表达式也不太可能真正正常工作。

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


【解决方案1】:

默认情况下,clang 使用 gcc 的标准 C++ 库 - libstdc++。它仍然不支持正则表达式。您可以尝试使用 libc++ - 另一个支持所有 C++11 的标准库实现。 clang++ -stdlib=libc++ source.cpp -o source 非常适合我使用教程中的第一个示例代码。

但是,将 libc++ 和 libstdc++ 库/可执行文件链接在一起并不是一个好主意 - 它们不兼容。

【讨论】:

    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2014-12-02
    • 2016-09-17
    • 2015-01-11
    相关资源
    最近更新 更多