Repository navigation
Expand file tree
/
Copy pathgeneral_concepts.cpp
More file actions
154 lines (120 loc) · 4.03 KB
/
Copy pathgeneral_concepts.cpp
File metadata and controls
154 lines (120 loc) · 4.03 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
142
143
144
145
146
147
148
149
150
151
152
153
154
//C++ top level concepts and language constructs
#include <chrono>
int main() {
std::chrono::seconds sec(5);
std::chrono::minutes min(2);
std::chrono::hours hr(1);
return 0;
}
std::chrono::system_clock: Represents the system-wide real time wall clock.
std::chrono::steady_clock: Represents a monotonic clock that is guaranteed to never be adjusted.
std::chrono::high_resolution_clock: Represents the clock with the shortest tick period.
int main() {
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
return 0;
}
// PImpl - Pointer to Implementation indiom
// header file
class MyClass {
public:
MyClass();
~MyClass();
void someMethod();
private:
class Impl;
Impl* pImpl;
};
// implementation file
class MyClass::Impl {
public:
void someMethod() { /* Implementation */ }
};
MyClass::MyClass() : pImpl(new Impl()) {}
MyClass::~MyClass() { delete pImpl; }
void MyClass::someMethod() { pImpl->someMethod(); }
//RAII
class Lock {
public:
Lock(std::mutex& mtx) : mutex_(mtx) {
mutex_.lock();
}
~Lock() {
mutex_.unlock();
}
private:
std::mutex& mutex_;
};
std::mutex some_mutex;
void protected_function() {
Lock lock(some_mutex);
// Do some work that must be synchronized
// No need to explicitly unlock the mutex, it will be automatically unlocked when lock goes out of scope.
}
//RAII
class ManagedArray {
public:
ManagedArray(size_t size) : size_(size), data_(new int[size]) {
}
~ManagedArray() {
delete[] data_;
}
// Access function
int& operator [](size_t i) {
return data_[i];
}
private:
size_t size_;
int* data_;
};
{
ManagedArray arr(10);
arr[0] = 42;
// No need to explicitly free memory, it will be automatically released when arr goes out of scope.
}
/*
C++ standards are a set of rules and guidelines that define the language’s features, syntax, and semantics.
The International Organization for Standardization (ISO) is responsible for maintaining and updating the
C++ standards. The main purpose of the standards is to ensure consistency, efficiency, and maintainability
across multiple platforms and compilers.
Here’s a brief summary of the different C++ standards released to date:
C++98/C++03: The first standardized version of C++, which introduced many features like templates,
exceptions, and the Standard Template Library (STL). C++03 is a minor update to C++98 with some bug
fixes and performance improvements.
C++11: A major upgrade to the language, which introduced features such as:
Lambda expressions:
auto sum = [](int a, int b) -> int { return a + b; };
Range-based for loops:
std::vector<int> numbers = {1, 2, 3, 4};
for (int num : numbers) {
std::cout << num << std::endl;
}
Smart pointers like std::shared_ptr and std::unique_ptr.
C++14: A minor update to C++11, which added features such as:
Generic lambda expressions:
auto generic_sum = [](auto a, auto b) { return a + b; };
Binary literals:
int binary_number = 0b1010;
C++17: Another major update that introduced features such as:
if and switch with initializers:
if (auto it = my_map.find(key); it != my_map.end()) {
// use 'it' here
}
Structured bindings:
std::map<std::string, int> my_map = {{"A", 1}, {"B", 2}};
for (const auto& [key, value] : my_map) {
// use 'key' and 'value' here
}
C++20: The latest major update to the language, with features such as:
Concepts:
template<typename T>
concept Addable = requires(T a, T b) {
{ a + b } -> std::same_as<T>;
};
Ranges:
std::vector<int> numbers = {1, 2, 3, 4};
auto doubled = numbers | std::views::transform([](int n) { return n * 2; });
Coroutines and more.
Remember that to use these language features, you might need to configure your compiler to use
the specific C++ standard version. For example, with GCC or Clang, you can use the -std=c++11,
-std=c++14, -std=c++17, or -std=c++20 flags.
*/