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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include <iostream>
#include <variant>
#include <string>
#include <vector>
#include <optional>
// 递归variant(需要前向声明)
struct JsonValue;
using JsonArray = std::vector<JsonValue>;
using JsonObject = std::map<std::string, JsonValue>;
struct JsonValue
{
std::variant<
std::nullptr_t,
bool,
int,
double,
std::string,
JsonArray,
JsonObject
> data;
JsonValue() : data(nullptr) {}
JsonValue(bool b) : data(b) {}
JsonValue(int i) : data(i) {}
JsonValue(double d) : data(d) {}
JsonValue(const std::string& s) : data(s) {}
JsonValue(const JsonArray& a) : data(a) {}
JsonValue(const JsonObject& o) : data(o) {}
};
// JSON打印访问者
struct JsonPrintVisitor
{
void operator()(std::nullptr_t) const
{
std::cout << "null";
}
void operator()(bool value) const
{
std::cout << (value ? "true" : "false");
}
void operator()(int value) const
{
std::cout << value;
}
void operator()(double value) const
{
std::cout << value;
}
void operator()(const std::string& value) const
{
std::cout << "\"" << value << "\"";
}
void operator()(const JsonArray& array) const
{
std::cout << "[";
for (size_t i = 0; i < array.size(); ++i)
{
if (i > 0) std::cout << ", ";
std::visit(*this, array[i].data);
}
std::cout << "]";
}
void operator()(const JsonObject& object) const
{
std::cout << "{";
bool first = true;
for (const auto& [key, value] : object)
{
if (!first) std::cout << ", ";
first = false;
std::cout << "\"" << key << "\": ";
std::visit(*this, value.data);
}
std::cout << "}";
}
};
// 异常安全的variant操作
template<typename... Types>
class SafeVariant
{
private:
std::variant<Types...> m_Data;
public:
template<typename T>
SafeVariant(T&& value) : m_Data(std::forward<T>(value)) {}
template<typename T>
std::optional<T> Get() const
{
if (std::holds_alternative<T>(m_Data))
{
return std::get<T>(m_Data);
}
return std::nullopt;
}
template<typename Visitor>
auto Visit(Visitor&& visitor) const
{
return std::visit(std::forward<Visitor>(visitor), m_Data);
}
size_t Index() const { return m_Data.index(); }
template<typename T>
bool Is() const { return std::holds_alternative<T>(m_Data); }
};
void AdvancedFeaturesDemo()
{
std::cout << "=== Advanced Features Demo ===" << std::endl;
// JSON示例
JsonValue json = JsonObject{
{"name", JsonValue("John Doe")},
{"age", JsonValue(30)},
{"active", JsonValue(true)},
{"scores", JsonValue(JsonArray{
JsonValue(95),
JsonValue(87),
JsonValue(92)
})},
{"address", JsonValue(JsonObject{
{"street", JsonValue("123 Main St")},
{"city", JsonValue("Anytown")}
})}
};
std::cout << "JSON: ";
std::visit(JsonPrintVisitor{}, json.data);
std::cout << std::endl;
// 安全variant示例
std::cout << "\nSafe variant demo:" << std::endl;
SafeVariant<int, double, std::string> safeVar(42);
auto intValue = safeVar.Get<int>();
auto stringValue = safeVar.Get<std::string>();
std::cout << "Int value: " << (intValue ? std::to_string(*intValue) : "none") << std::endl;
std::cout << "String value: " << (stringValue ? *stringValue : "none") << std::endl;
// 类型检查
std::cout << "Is int: " << safeVar.Is<int>() << std::endl;
std::cout << "Is string: " << safeVar.Is<std::string>() << std::endl;
// 访问者模式
safeVar.Visit([](const auto& value) {
std::cout << "Safe variant contains: " << value << std::endl;
});
}
void BestPracticesDemo()
{
std::cout << "\n=== Best Practices ===" << std::endl;
std::cout << "1. Use std::variant instead of union for type safety" << std::endl;
std::cout << "2. Prefer std::visit over manual type checking" << std::endl;
std::cout << "3. Use std::holds_alternative for simple type checks" << std::endl;
std::cout << "4. Consider std::optional for nullable types" << std::endl;
std::cout << "5. Use generic lambdas for simple visitors" << std::endl;
std::cout << "6. Be careful with recursive variants (use pointers/smart pointers)" << std::endl;
std::cout << "7. Consider performance implications of large variants" << std::endl;
}
int main()
{
AdvancedFeaturesDemo();
BestPracticesDemo();
return 0;
}
|