From 36b9ded8175bd5fbdac9ce8f885da1fbb80762c5 Mon Sep 17 00:00:00 2001 From: Marco Mariotto Date: Tue, 13 Oct 2026 21:54:39 +0200 Subject: [PATCH] fix: ids are now reset correctly for the next match statement --- develop/matchit/patterns.h | 24 +++++++++++++----------- include/matchit.h | 24 +++++++++++++----------- test/matchit/id.cpp | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/develop/matchit/patterns.h b/develop/matchit/patterns.h index 7e3fc53..e4660bc 100644 --- a/develop/matchit/patterns.h +++ b/develop/matchit/patterns.h @@ -329,6 +329,8 @@ namespace matchit return matchPattern(std::forward(value), mPattern, /*depth*/ 0, context); } + + constexpr const Pattern& pattern() const {return mPattern;} constexpr auto execute() const { return mHandler(); } private: @@ -1658,7 +1660,7 @@ namespace matchit static_assert(PatternTraits>::nbIdV == 0); template - constexpr auto matchPatterns(Value &&value, PatternPairs const &...patterns) + constexpr auto matchPatterns(Value &&value, PatternPairs const &...pattern_pairs) { using RetType = typename PatternPairsRetType::RetType; using TypeTuple = decltype(std::tuple_cat( @@ -1669,19 +1671,19 @@ namespace matchit if constexpr (!std::is_same_v) { constexpr auto const func = - [](auto const &pattern, auto &&value, RetType &result) constexpr->bool + [](auto const &pattern_pair, auto &&value, RetType &result) constexpr->bool { auto context = typename ContextTrait::ContextT{}; - if (pattern.matchValue(std::forward(value), context)) + if (pattern_pair.matchValue(std::forward(value), context)) { - result = pattern.execute(); - processId(pattern, 0, IdProcess::kCANCEL); + result = pattern_pair.execute(); + processId(pattern_pair.pattern(), 0, IdProcess::kCANCEL); return true; } return false; }; RetType result{}; - bool const matched = (func(patterns, value, result) || ...); + bool const matched = (func(pattern_pairs, value, result) || ...); if (!matched) { throw std::logic_error{"Error: no patterns got matched!"}; @@ -1692,18 +1694,18 @@ namespace matchit else // statement, no return value, mismatching all patterns is not an error. { - auto const func = [](auto const &pattern, auto &&value) -> bool + auto const func = [](auto const &pattern_pair, auto &&value) -> bool { auto context = typename ContextTrait::ContextT{}; - if (pattern.matchValue(std::forward(value), context)) + if (pattern_pair.matchValue(std::forward(value), context)) { - pattern.execute(); - processId(pattern, 0, IdProcess::kCANCEL); + pattern_pair.execute(); + processId(pattern_pair.pattern(), 0, IdProcess::kCANCEL); return true; } return false; }; - bool const matched = (func(patterns, value) || ...); + bool const matched = (func(pattern_pairs, value) || ...); static_cast(matched); } } diff --git a/include/matchit.h b/include/matchit.h index f3df93b..5e28abe 100644 --- a/include/matchit.h +++ b/include/matchit.h @@ -649,6 +649,8 @@ namespace matchit return matchPattern(std::forward(value), mPattern, /*depth*/ 0, context); } + + constexpr const Pattern& pattern() const {return mPattern;} constexpr auto execute() const { return mHandler(); } private: @@ -1978,7 +1980,7 @@ namespace matchit static_assert(PatternTraits>::nbIdV == 0); template - constexpr auto matchPatterns(Value &&value, PatternPairs const &...patterns) + constexpr auto matchPatterns(Value &&value, PatternPairs const &...pattern_pairs) { using RetType = typename PatternPairsRetType::RetType; using TypeTuple = decltype(std::tuple_cat( @@ -1989,19 +1991,19 @@ namespace matchit if constexpr (!std::is_same_v) { constexpr auto const func = - [](auto const &pattern, auto &&value, RetType &result) constexpr->bool + [](auto const &pattern_pair, auto &&value, RetType &result) constexpr->bool { auto context = typename ContextTrait::ContextT{}; - if (pattern.matchValue(std::forward(value), context)) + if (pattern_pair.matchValue(std::forward(value), context)) { - result = pattern.execute(); - processId(pattern, 0, IdProcess::kCANCEL); + result = pattern_pair.execute(); + processId(pattern_pair.pattern(), 0, IdProcess::kCANCEL); return true; } return false; }; RetType result{}; - bool const matched = (func(patterns, value, result) || ...); + bool const matched = (func(pattern_pairs, value, result) || ...); if (!matched) { throw std::logic_error{"Error: no patterns got matched!"}; @@ -2012,18 +2014,18 @@ namespace matchit else // statement, no return value, mismatching all patterns is not an error. { - auto const func = [](auto const &pattern, auto &&value) -> bool + auto const func = [](auto const &pattern_pair, auto &&value) -> bool { auto context = typename ContextTrait::ContextT{}; - if (pattern.matchValue(std::forward(value), context)) + if (pattern_pair.matchValue(std::forward(value), context)) { - pattern.execute(); - processId(pattern, 0, IdProcess::kCANCEL); + pattern_pair.execute(); + processId(pattern_pair.pattern(), 0, IdProcess::kCANCEL); return true; } return false; }; - bool const matched = (func(patterns, value) || ...); + bool const matched = (func(pattern_pairs, value) || ...); static_cast(matched); } } diff --git a/test/matchit/id.cpp b/test/matchit/id.cpp index 287f691..c6d48ad 100644 --- a/test/matchit/id.cpp +++ b/test/matchit/id.cpp @@ -307,3 +307,18 @@ TEST(Id, move) auto y = std::move(*x); EXPECT_TRUE((*x).empty()); } + +TEST(Id, resetIdForNextMatch) +{ + Id x; + + match(2)( + pattern | x = [&](){return *x;} + ); + + EXPECT_NO_THROW( + match(3)( + pattern | x = [&](){return *x;} + ) + ); +}