-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add qxFuncAggregate, useful for std::variant overload pattern
- Loading branch information
1 parent
06c1ffe
commit 3299eff
Showing
3 changed files
with
62 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! [0] | ||
#include <iostream> | ||
#include <string> | ||
#include <variant> | ||
#include <vector> | ||
#include <functional> | ||
#include <qx/utility/qx-helpers.h> | ||
|
||
// The variant to visit | ||
using var_t = std::variant<int, long, double, bool, char, std::string>; | ||
|
||
int main() | ||
{ | ||
std::vector<var_t> vec = {10, 15l, 1.5, true, 'c', "hello"}; | ||
|
||
std::function<void(std::string)> f = [](std::string arg) { std::cout << arg << " (from string std::function)"; }; | ||
|
||
for (auto& v : vec) | ||
{ | ||
// Visit using aggregate | ||
std::visit(qxFuncAggregate{ | ||
[](auto arg) { std::cout << arg << " (from catch-all lambda) "; }, | ||
[](double arg) { std::cout << std::fixed << arg << " (from double lambda) "; }, | ||
[](bool arg) { std::cout << arg << " (from bool lambda) "; }, | ||
[](char arg) { std::cout << arg << " (from char lambda) "; }, | ||
f | ||
}, v); | ||
|
||
std::cout << std::endl; | ||
} | ||
} | ||
|
||
// 10 (from catch-all lambda) | ||
// 15 (from catch-all lambda) | ||
// 1.500000 (from double lambda) | ||
// 1 (from bool lambda) | ||
// c (from char lambda) | ||
// hello (from string std::function) | ||
//! [0] |
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