Skip to content

basic_regex::assign resets locale -> imbue has no effect #123309

Open
@Flamefire

Description

@Flamefire

As per the standard a basic_regex::imbue call resets the stored pattern (i.e. "The regular expression does not match any character sequence after the call.")

Hence a call to assign or the assignment operator is required AFTER imbue.

However the way assign is implemented it resets the locale to the global locale as all end up calling the overload constructing a new regex and assigning it to "this":

return assign(basic_regex(__first, __last, __f));

Hence there is no way to change the locale.

Sample code:

#include <locale>
#include <regex>
#include <cassert>

int test_main(int /*argc*/, char** /*argv*/)
{
    std::locale a("");
    std::locale b = std::locale::classic();
    std::locale::global(a);
    std::regex r;
    assert(r.getloc() == a);
   // Change to b and verify
    assert(r.imbue(b) == a);
    assert(r.getloc() == b);
    r = "pattern";
    assert(r.getloc() == b); // Fires
    assert(r.getloc() == a); // Unexpected success
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.regexIssues related to regex

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions