I'm compiling this with VS 2022 C++ compiler, as "ISO C++20 Standard (/std:c++20)" for a Debug configuration.
How come this throws the following std::regex_error:
regex_error(error_backref): The expression contained an invalid back reference.
std::wregex re;
bool bCaseSens = true;
re.assign(L"FilE",
std::wregex::ECMAScript |
std::wregex::nosubs |
std::wregex::optimize |
(bCaseSens ? 0 : std::wregex::icase)
);
but this compiles and works fine?
std::wregex re;
bool bCaseSens = true;
re.assign(L"FilE",
std::wregex::ECMAScript |
std::wregex::nosubs |
std::wregex::optimize
);
PS. Note that the Release build doesn't throw that exception.
PS2. Also tried with older versions of C++ standard with the same effects.
assign(4) because the0converts the expression to astd::size_t(or something convertible into that), but in the second case it'sassign(3). q.v. en.cppreference.com/w/cpp/regex/basic_regex/assign.html(bCaseSens ? 0 : std::wregex::icase)isint, notstd::regex::syntax_option_type. That means that the type of the result of the sequence of|operators is alsoint, and notsyntax_option_type, so the compiler picks an inappropriate overload ofassign.std::wregex re("");thenre.assign()What I ended up adopting was to use a copy assign method, ex :typedef boost::wregex X_regex;\n #define MAKEREGEX(str,options) boost::wregex(str,options)\n X_regex re = MAKEREGEX( "\\bFilE\\b", ECMAScript | nosubs | optimize | (bCaseSens ? 0 : icase));