forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proxy_type.cpp
More file actions
49 lines (37 loc) · 1.75 KB
/
test_proxy_type.cpp
File metadata and controls
49 lines (37 loc) · 1.75 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
#include <iostream>
#include <nlohmann/json.hpp>
#include <typeinfo>
int main() {
nlohmann::json j = {{"name", "Alice"}, {"age", 30}};
std::cout << "=== 测试代理对象的类型 ===\n\n";
// 1. 直接看 j["name"] 的类型
auto proxy = j["name"];
std::cout << "1. proxy 的原始类型名: " << typeid(proxy).name() << "\n";
std::cout << " (编译器会显示混淆后的名字)\n\n";
// 2. 检查它是不是 json 类型
std::cout << "2. proxy 是 nlohmann::json 类型吗? "
<< std::boolalpha
<< (typeid(proxy) == typeid(nlohmann::json)) << "\n\n";
// 3. 证明它可以转换成不同类型
std::cout << "3. 同一个 proxy 可以转成不同类型:\n";
nlohmann::json j2 = {{"value", 42}};
auto multi_proxy = j2["value"];
std::string as_string = multi_proxy; // 转成 string
int as_int = multi_proxy; // 转成 int
double as_double = multi_proxy; // 转成 double
std::cout << " 作为 string: " << as_string << "\n";
std::cout << " 作为 int: " << as_int << "\n";
std::cout << " 作为 double: " << as_double << "\n\n";
// 4. 证明它是引用(修改会影响原 json)
std::cout << "4. 证明它是引用:\n";
std::cout << " 修改前: " << j["name"] << "\n";
auto& ref = j["name"];
ref = "Bob"; // 通过引用修改
std::cout << " 修改后: " << j["name"] << "\n";
std::cout << " 原始 json: " << j.dump() << "\n\n";
// 5. 对比:如果不用 auto,显式写类型
std::cout << "5. 显式类型声明:\n";
nlohmann::json& explicit_ref = j["age"];
std::cout << " nlohmann::json& 类型的值: " << explicit_ref << "\n";
return 0;
}