-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |