forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariant.cpp
More file actions
41 lines (32 loc) · 1006 Bytes
/
Copy pathvariant.cpp
File metadata and controls
41 lines (32 loc) · 1006 Bytes
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
#include<iostream>
#include<variant>
using namespace std;
void checkType(variant<int, double, string> v);
int main() {
variant<int, double, string> values;
values = 10;
cout << "Values = " << get<int>(values) << endl;
values = 83.242;
cout << "Values = " << get<double>(values) << endl;
values = "HELLO";
cout << "Values = " << get<string>(values) << endl;
cout << endl;
checkType(values);
values = 3.14;
checkType(values);
values = 7;
checkType(values);
return 0;
}
// Check to see what type a variant holds
void checkType(variant<int, double, string> v) {
if (holds_alternative<int>(v)) {
cout << "The parameter holds an int value = " << get<int>(v) << endl;
}
if (holds_alternative<double>(v)) {
cout << "The parameter holds an double value = " << get<double>(v) << endl;
}
if (holds_alternative<string>(v)) {
cout << "The parameter holds an string value = " << get<string>(v) << endl;
}
}