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
|
#include <iostream>
#include <string>
#include <vector>
#include <utility>
// 演示左值和右值在函数重载中的应用
class StringProcessor
{
public:
// 处理左值字符串(可能需要拷贝)
void process(const std::string& str)
{
std::cout << "Processing lvalue string: " << str << std::endl;
// 这里可能需要拷贝字符串
data = str; // 拷贝
}
// 处理右值字符串(可以移动)
void process(std::string&& str)
{
std::cout << "Processing rvalue string: " << str << std::endl;
// 这里可以移动字符串,避免拷贝
data = std::move(str); // 移动
}
void printData() const
{
std::cout << "Stored data: " << data << std::endl;
}
private:
std::string data;
};
// 完美转发示例
template<typename T>
void forwardToProcessor(StringProcessor& processor, T&& str)
{
std::cout << "Forwarding to processor..." << std::endl;
processor.process(std::forward<T>(str));
}
// 工厂函数示例
template<typename T, typename... Args>
T createObject(Args&&... args)
{
std::cout << "Creating object with perfect forwarding..." << std::endl;
return T(std::forward<Args>(args)...);
}
class Person
{
public:
std::string name;
int age;
Person(const std::string& n, int a) : name(n), age(a)
{
std::cout << "Person constructor (lvalue string): " << name << std::endl;
}
Person(std::string&& n, int a) : name(std::move(n)), age(a)
{
std::cout << "Person constructor (rvalue string): " << name << std::endl;
}
};
void PracticalApplicationsDemo()
{
std::cout << "=== Practical Applications Demo ===" << std::endl;
// 1. 函数重载优化
std::cout << "\n1. Function Overload Optimization:" << std::endl;
StringProcessor processor;
std::string lvalueStr = "Hello World";
processor.process(lvalueStr); // 调用左值版本
processor.process("Temporary String"); // 调用右值版本
processor.process(std::string("Another Temp")); // 调用右值版本
processor.printData();
// 2. 完美转发
std::cout << "\n2. Perfect Forwarding:" << std::endl;
StringProcessor processor2;
std::string anotherStr = "Forwarded String";
forwardToProcessor(processor2, anotherStr); // 转发左值
forwardToProcessor(processor2, "Forwarded Temp"); // 转发右值
processor2.printData();
// 3. 工厂函数
std::cout << "\n3. Factory Function:" << std::endl;
std::string personName = "Alice";
auto person1 = createObject<Person>(personName, 25); // 左值
auto person2 = createObject<Person>("Bob", 30); // 右值
auto person3 = createObject<Person>(std::string("Charlie"), 35); // 右值
std::cout << "Person 1: " << person1.name << ", " << person1.age << std::endl;
std::cout << "Person 2: " << person2.name << ", " << person2.age << std::endl;
std::cout << "Person 3: " << person3.name << ", " << person3.age << std::endl;
}
// 容器操作示例
void ContainerOperationsDemo()
{
std::cout << "\n=== Container Operations Demo ===" << std::endl;
std::vector<std::string> strings;
// 使用左值
std::string str1 = "First String";
strings.push_back(str1); // 拷贝
std::cout << "After push_back lvalue, str1: " << str1 << std::endl;
// 使用右值
strings.push_back("Second String"); // 移动(如果可能)
// 显式移动
std::string str2 = "Third String";
strings.push_back(std::move(str2)); // 移动
std::cout << "After move, str2: '" << str2 << "' (moved-from state)" << std::endl;
// 使用emplace_back(直接构造)
strings.emplace_back("Fourth String"); // 直接构造
std::cout << "Vector contents:" << std::endl;
for (size_t i = 0; i < strings.size(); ++i)
{
std::cout << " [" << i << "]: " << strings[i] << std::endl;
}
}
int main()
{
PracticalApplicationsDemo();
ContainerOperationsDemo();
return 0;
}
|