3

I'm trying to follow this tutorial about regex in C++11. When I try to compile the little code example I get these errors

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.

What's wrong?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Davide Aversa
  • 5,628
  • 6
  • 28
  • 40
  • It isn't a constexpr it is just a const why would you use constexpr there? You aren't computing anything. That said I think the real problem is that it doesn't think 1 is a literal of the correct type??? since you are n't computing the values just change it to a const? – John Sobolewski Sep 12 '12 at 11:43
  • The problem is that these errors stay in the "regex" header file. Is a standard library file, i cannot change constexpr with const. :| – Davide Aversa Sep 12 '12 at 13:22
  • 2
    `match_default = 0;` sure looks like a constant expression to me. I wonder if this is just a bug in your version of Clang? What versions of Clang and GCC (which I assume is providing your libstdc++) are you using? – Josh Kelley Sep 12 '12 at 20:22
  • 4
    Try to use libc++ instead of libstdc++. – kennytm Sep 13 '12 at 01:21
  • 1
    No Kenny. It gave me the same error. I'm starting to think that there are some problems in this version of clang/libstdc++/Ubuntu. I will try with another cofiguration... – Davide Aversa Sep 17 '12 at 19:10
  • 4
    libstdc++'s support for c++11 regex's are fundamentally broken currently. See http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011 for details. Even if you get the code to compile, even the simplest regex is not likely to actually work correctly. – Nathan Ernst Nov 29 '12 at 19:10
  • @DavideAversa, using libc++ **cannot possibly** give the same error, because those errors are in libstdc++ headers. If you use libc++ instead of libstdc++ then you can't include those headers. If you are getting errors from those headers then **you are not using libc++** – Jonathan Wakely May 22 '13 at 09:15

1 Answers1

0

By default clang uses gcc's standard C++ library - libstdc++. It still does not support regular expressions. You can try using libc++ - another standard library implementation which supports all of C++11. clang++ -stdlib=libc++ source.cpp -o source works perfectly for me with the first example code from the tutorial.

However it's not a good idea to link libc++ and libstdc++ libraries/executables together - they are incompatible.

Hristo Venev
  • 972
  • 5
  • 17