mardi 4 avril 2017

c++ regex match_results life cycle

The first test passes, while the second fails.

TEST_METHOD(ShouldMatchText) {
    std::wsmatch match;
    std::wstring text(L"abc");
    std::regex_match(text, match, std::wregex(L"(.*)"));
    Assert::AreEqual(std::wstring(L"abc"), std::wstring(match[1]));
}

TEST_METHOD(ShouldMatchText2) {
    std::wsmatch match;
    {
        std::wstring text(L"abc");
        std::regex_match(text, match, std::wregex(L"(.*)"));
    }
    Assert::AreEqual(std::wstring(L"abc"), std::wstring(match[1]));
}

It seems that match_results refers to matching string internally. Is there a way to make math_results valid even after the matching string de-allocated? Because in my real case, the matching string is temp, and I do want to use the matched groups later in other scopes.

Aucun commentaire:

Enregistrer un commentaire