40 using Variant_t = std::variant<int, char, std::string, QString, double, float>;
56 void VisitorTest::testBasicVisitor ()
59 const auto& res =
Visit (v,
60 [] (
char) {
return true; },
61 [] (int) {
return false; },
62 [] (std::string) {
return false; },
63 [] (QString) {
return false; },
64 [] (double) {
return false; },
65 [] (float) {
return false; });
69 void VisitorTest::testBasicVisitorGenericFallback ()
72 const auto& res =
Visit (v,
73 [] (
char) {
return true; },
74 [] (int) {
return false; },
75 [] (
auto) {
return false; });
79 void VisitorTest::testBasicVisitorCoercion ()
82 const auto& res =
Visit (v,
83 [] (
int) {
return true; },
84 [] (std::string) {
return false; },
85 [] (QString) {
return false; },
86 [] (double) {
return false; },
87 [] (float) {
return false; });
91 void VisitorTest::testBasicVisitorCoercionGenericFallback ()
94 const auto& res =
Visit (v,
95 [] (
int) {
return false; },
96 [] (QString) {
return false; },
97 [] (
auto) {
return true; });
101 #define NC nc = std::unique_ptr<int> {} 103 void VisitorTest::testNonCopyableFunctors ()
106 const auto& res =
Visit (v,
107 [
NC] (
char) {
return true; },
108 [
NC] (int) {
return false; },
109 [
NC] (std::string) {
return false; },
110 [
NC] (QString) {
return false; },
111 [
NC] (double) {
return false; },
112 [
NC] (float) {
return false; });
113 QCOMPARE (res,
true);
117 void VisitorTest::testAcceptsRValueRef ()
120 [] (char) {
return true; },
121 [] (
auto) {
return false; });
122 QCOMPARE (res,
true);
125 void VisitorTest::testLValueRef ()
129 auto& res =
Visit (v, [&ref] (
auto) ->
int& {
return ref; });
134 void VisitorTest::testLValueRef2 ()
137 Visit (v, [] (
auto& s) ->
int& {
return s.field1; }) = 10;
138 const auto& res =
Visit (v, [] (
const auto& s) ->
const int& {
return s.field1; });
142 void VisitorTest::testPrepareVisitor ()
147 [] (char) {
return true; },
148 [] (int) {
return false; },
149 [] (std::string) {
return false; },
150 [] (QString) {
return false; },
151 [] (double) {
return false; },
152 [] (float) {
return false; }
155 const auto& res = visitor (v);
156 QCOMPARE (res,
true);
159 void VisitorTest::testPrepareVisitorConst ()
164 [] (char) {
return true; },
165 [] (int) {
return false; },
166 [] (std::string) {
return false; },
167 [] (QString) {
return false; },
168 [] (double) {
return false; },
169 [] (float) {
return false; }
172 const auto& res = visitor (v);
173 QCOMPARE (res,
true);
176 void VisitorTest::testPrepareVisitorRValue ()
180 [] (char) {
return true; },
181 [] (int) {
return false; },
182 [] (std::string) {
return false; },
183 [] (QString) {
return false; },
184 [] (double) {
return false; },
185 [] (float) {
return false; }
188 const auto& res = visitor (
Variant_t {
'a' });
189 QCOMPARE (res,
true);
192 void VisitorTest::testPrepareVisitorFinally ()
200 [] (char) {
return true; },
201 [] (
auto) {
return false; }
202 }.Finally ([&fin] { fin =
true; });
204 const auto& res = visitor (v);
205 QCOMPARE (res,
true);
206 QCOMPARE (fin,
true);
209 void VisitorTest::testPrepareJustAutoVisitor ()
211 using Variant_t = std::variant<int, double, float>;
215 [] (
auto e) {
return std::to_string (e); }
218 const auto& res = visitor (
Variant_t { 10 });
219 QCOMPARE (res, std::string {
"10" });
222 void VisitorTest::testPrepareRecursiveVisitor ()
224 using SubVariant_t = std::variant<int, double, float>;
225 using Variant_t = std::variant<SubVariant_t, QString>;
229 [] (
const QString& str) {
return str; },
230 Visitor { [] (
auto e) {
return QString::fromStdString (std::to_string (e)); } }
233 const auto& res = visitor (
Variant_t { SubVariant_t { 10 } });
234 QCOMPARE (res, QString {
"10" });
237 void VisitorTest::testPrepareVisitorMutable ()
242 [] (int)
mutable {
return true; },
243 [] (
auto)
mutable {
return false; }
246 const auto& res = visitor (v);
247 QCOMPARE (res,
false);
auto Visit(const Either< Left, Right > &either, Args &&... args)
std::variant< int, char, std::string, QString, double, float > Variant_t
std::variant< S1, S2 > SVariant_t
Visitor(Args &&...) -> Visitor< Void, Args... >