3

I am trying to compile the following C++ code using the command:

g++ -std=c++17 -o rgx rgx.cpp

#include <iostream>
#include <regex>
#include <string>
using namespace std;

int main() {
        regex rgx("^([A-Z]*)$", regex::ECMAScript | regex::multiline | regex::icase);
        smatch rmtch;
        string str = "AbcefgH\r\nTest";
        if (regex_match(str, rmtch, rgx)) {
                for (size_t i = 0; i < rmtch.size(); i++) {
                        ssub_match smtch = rmtch[i];
                        string s_m = smtch.str();
                        cout << " submatch " << i << ": " << s_m << endl;
                }
        }
        return 0;
};

However get the following compile time error

rgx.cpp: In function ‘int main()’:
rgx.cpp:7:53: error: ‘multiline’ is not a member of ‘std::__cxx11::regex’ {aka ‘std::__cxx11::basic_regex<char>’}
    7 |  regex rgx("^([A-Z]*)$", regex::ECMAScript | regex::multiline | regex::icase);
g++ --version
g++ (Ubuntu 9.1.0-8ubuntu1) 9.1.0

Why is g++ using __cxx11::regex when I've specified -std=c++17?

cppreference.com/w/cpp/regex/basic_regex defines regex::multiline as being part of the C++17 standard

IcySi
  • 43
  • 2
  • Looks like the standard library you're using hasn't been updated to include all the C++17 additions yet. – Jerry Coffin Jul 16 '19 at 00:01
  • If this is the case, how am I supposed to use multiline regex? – IcySi Jul 16 '19 at 00:28
  • One obvious choice would be to find a different regex library that supports what you want. Another would be to recognize a multiline regex as a number of consecutive single-line regexes. – Jerry Coffin Jul 16 '19 at 05:15

1 Answers1

0

Libstdc++ doesn't appear to implement regex::multiline.

Note that the fact that it's using std::__cxx11::regex doesn't mean that you're stuck in the past. __cxx11 is not strictly reserved to C++11 features. The purpose of inline namespaces (such as __cxx11) is that if eventually a class/struct needs to change in ways that are not backwards compatible, new programs will grab its definition from a new inline namespace while the old definition remains for programs that were built before.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • Interesting, I'm wondering why that would be if I'm running on the latest gcc build from eoan ubuntu packages repository? – IcySi Jul 16 '19 at 00:09