Skip to content

Commit

Permalink
Fix #68 : Store value instead of ref in PatternPair. (#70)
Browse files Browse the repository at this point in the history
* Fix #68

* Accept function type

Co-authored-by: Bowen Fu <missing>
  • Loading branch information
BowenFu committed Oct 27, 2021
1 parent 3802273 commit 8c4fbf3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions develop/matchit/patterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ namespace matchit
constexpr auto execute() const { return mHandler(); }

private:
Pattern const &mPattern;
Func const &mHandler;
Pattern const mPattern;
Func const mHandler;
};

template <typename Pattern, typename Pred>
Expand Down
4 changes: 2 additions & 2 deletions include/matchit.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ namespace matchit
constexpr auto execute() const { return mHandler(); }

private:
Pattern const &mPattern;
Func const &mHandler;
Pattern const mPattern;
std::conditional_t<std::is_function_v<Func>, Func const&, Func const> mHandler;
};

template <typename Pattern, typename Pred>
Expand Down
1 change: 1 addition & 0 deletions sample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Extractor-Pattern
Predicate-based-Discriminator
Closed-Class-Hierarchy
Matcher-within
visit
)

foreach(sample ${MATCHIT_SAMPLES})
Expand Down
52 changes: 52 additions & 0 deletions sample/visit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "matchit.h"

#include <string>
#include <iostream>

template <typename T>
auto constexpr visitPat = [](auto func)
{
using namespace matchit;

return as<T>(meet([func](auto&& param) {
func(param);
return true;
}));
};

template <typename T>
auto constexpr visit = [](auto func)
{
using namespace matchit;

return pattern | visitPat<T>(func) = []{};
};

template <typename T>
void print(T&& x)
{
using namespace matchit;

match(x)(
visit<std::string>(
[](const std::string& text) {
std::cout << "Text message: " << text << std::endl;
}),
visit<int32_t>(
[](const int32_t num) {
std::cout << "Number: " << num << std::endl;
})
);
}

int main()
{
using namespace matchit;

std::variant<int32_t, std::string> v1 = "123";
std::variant<int32_t, std::string> v2 = 123;

print(v1);
print(v2);
return 0;
}

0 comments on commit 8c4fbf3

Please sign in to comment.