-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp11_demo.cpp
More file actions
141 lines (117 loc) · 3.28 KB
/
Copy pathcpp11_demo.cpp
File metadata and controls
141 lines (117 loc) · 3.28 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
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
// cpp11_demo.cpp
#include <iostream>
#include <memory>
#include <string>
#include <vector>
// ---------- Feature 1: auto & decltype ----------
auto add(int a, int b) -> int
{ // trailing return type (C++11)
return a + b;
}
decltype(42) get_forty_two()
{ // decltype deduces int
return 42;
}
void demo_auto_decltype()
{
std::cout << "\n=== auto & decltype ===\n";
auto x = add(3, 4); // auto = int
decltype(3.14) y = 3.14; // double
decltype(x) z = x;
std::cout << "auto x = " << x << "\n";
std::cout << "decltype(3.14) y = " << y << "\n";
std::cout << "decltype(x) z = " << z << "\n";
}
// ---------- Feature 2: range-based for loops ----------
void demo_range_for()
{
std::cout << "\n=== range-based for loops ===\n";
std::vector<int> vec{1, 2, 3, 4, 5};
int sum = 0;
// Clean iteration (no iterators!)
for (int val : vec) {
sum += val;
}
std::cout << "sum = " << sum << "\n";
// Modifiable refs
for (int &val : vec) {
++val;
}
std::cout << "vec now: ";
for (int val : vec) {
std::cout << val << " ";
}
std::cout << "\n";
}
// ---------- Feature 3: lambda expressions ----------
void demo_lambda()
{
std::cout << "\n=== lambda expressions ===\n";
int factor = 10;
std::vector<int> vec{1, 2, 3};
// Capture by value/reference
auto multiply = [factor](int x) { return x * factor; }; // [=] or [&] for all
for (int val : vec) {
std::cout << "multiply(" << val << ") = " << multiply(val) << "\n";
}
}
// ---------- Feature 4: nullptr ----------
void demo_nullptr()
{
std::cout << "\n=== nullptr ===\n";
int *ptr = nullptr; // type-safe null (not 0)
std::cout << "ptr == nullptr: " << (ptr == nullptr) << "\n";
}
// ---------- Feature 5: smart pointers ----------
struct DemoObj
{
int value;
DemoObj(int v)
: value(v)
{
std::cout << "DemoObj(" << v << ") created\n";
}
~DemoObj() { std::cout << "~DemoObj() destroyed\n"; }
};
void demo_smart_ptrs()
{
std::cout << "\n=== smart pointers ===\n";
// unique_ptr: exclusive ownership
{
auto up = std::make_unique<DemoObj>(
1); // C++14 make_unique, but unique_ptr raw new ok in C++11
std::cout << "unique_ptr value: " << up->value << "\n";
// auto-destroys on scope exit
} // destroyed here
// shared_ptr: shared ownership
std::shared_ptr<DemoObj> sp1 = std::make_shared<DemoObj>(2);
{
std::shared_ptr<DemoObj> sp2 = sp1; // refcount=2
std::cout << "shared_ptr value: " << sp2->value << "\n";
} // refcount=1
// sp1 destroyed after main
}
// ---------- Feature 6: constexpr & static_assert ----------
constexpr int factorial(int n)
{
return (n <= 1) ? 1 : (n * factorial(n - 1));
}
void demo_constexpr_assert()
{
std::cout << "\n=== constexpr & static_assert ===\n";
static_assert(factorial(5) == 120, "factorial(5) must be 120");
std::cout << "factorial(5) = " << factorial(5) << "\n"; // compile-time computable
}
// ---------- main ----------
int main()
{
std::cout << "C++11 feature tour\n";
demo_auto_decltype();
demo_range_for();
demo_lambda();
demo_nullptr();
demo_smart_ptrs();
demo_constexpr_assert();
std::cout << "\nDone.\n";
return 0;
}