“Multiline”不是“std::__cxx11::regex”的成员

‘multiline’ is not a member of ‘std::__cxx11::regex’

本文关键字:成员 regex cxx11 Multiline 不是 std      更新时间:2023-10-16

我正在尝试使用以下命令编译以下C++代码:

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 = "AbcefgHrnTest";
        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;
};

但是得到以下编译时错误

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

为什么 g++ 在我指定 -std=c++17 时使用 __cxx11::regex?

cppreference.com/w/cpp/regex/basic_regex 将regex::multiline定义为C++17标准的一部分

Libstdc++似乎没有实现regex::multiline

请注意,它使用std::__cxx11::regex的事实并不意味着您停留在过去。 __cxx11并不严格保留给 C++11 功能。内联命名空间(如 __cxx11(的目的是,如果最终类/结构需要以不向后兼容的方式进行更改,新程序将从新的内联命名空间中获取其定义,而旧定义保留用于以前构建的程序。