Repository navigation
Expand file tree
/
Copy pathtest_variant_visit.cc
More file actions
50 lines (44 loc) · 1.57 KB
/
Copy pathtest_variant_visit.cc
File metadata and controls
50 lines (44 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <variant>
struct Visitor {
void operator()(int i) const { std::cout << "int " << i << "\n"; }
void operator()(float f) const { std::cout << "float " << f << "\n"; }
void operator()(std::string s) const { std::cout << "string " << s << "\n"; }
};
void test_visitor_functor() {
std::variant<int, float, std::string> var;
var = 1;
std::visit(Visitor(), var);
var = 2.90f;
std::visit(Visitor(), var);
var = std::string("hello world");
std::visit(Visitor(), var);
}
void test_visitor_lambda() {
std::variant<int, float, std::string> var;
var = 1;
std::visit([](const auto& value) { std::cout << "value " << value << "\n"; }, var);
var = 2.90f;
std::visit([](const auto& value) { std::cout << "value " << value << "\n"; }, var);
var = std::string("hello world");
std::visit([](const auto& value) { std::cout << "value " << value << "\n"; }, var);
var = std::string("hello type");
std::visit(
[](const auto& value) {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int>) {
std::cout << "int value " << value << "\n";
} else if constexpr (std::is_same_v<T, float>) {
std::cout << "float value " << value << "\n";
} else if constexpr (std::is_same_v<T, std::string>) {
std::cout << "string value " << value << "\n";
}
},
var);
}
int main() {
test_visitor_functor();
std::cout << "=========== \n";
test_visitor_lambda();
}