Skip to content

Commit 6b326a4

Browse files
committed
tones of updates
1 parent fbe8a31 commit 6b326a4

21 files changed

+2467
-19
lines changed

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
105105
* [Allocator](docs/allocator.md)
106106
* [Algorithms library](src/algorithms_library.cpp)
107107
* [Assert](docs/assert.md)
108-
* [Atomic operations and Types](docs/atomic.md)
108+
* [Atomic operations and Atomic Types](docs/atomic.md)
109109
* [Asynchronous programming](docs/asynchronous_programming.md)
110110
* [Attribute specifier sequence [[ attribute-list ]] ](docs/attribute_specifier_sequence.md)
111111
* [Basic IO Operation, Streams, Reading/Writing Files, Formatting Output, cin, scanf, gets, getline, printf](docs/basic_IO_operation.md)
@@ -121,7 +121,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
121121
* [set, map, pair, tuple, tie, unordered_map, multimap, unordered_set, multiset](docs/set_map_pair_tuple.md)
122122
* [stack, queue, priority_queue, deque](docs/stack_queue_priority_queue_deque.md)
123123
* [Const, Constexpr and Mutable](docs/const_constexpr_mutable.md)
124-
* [Data Types, Numerical Limits, Precision](docs/primitive_data_types_numerical_limits_accuracy.md)
124+
* [Data Types, Numerical Limits, Machine Epsilon, Precision](docs/primitive_data_types_numerical_limits_machine_epsilon_precision.md)
125125
* [Data Types Conversions, Casting](docs/type_conversions_casting.md)
126126
* [Decay](docs/std_decay.md)
127127
* [Dynamic Memory Allocation in C](docs/dynamic_memory_allocation.md)
@@ -147,7 +147,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
147147
* [Optional](src/optional.cpp)
148148
* [Parameter Pack Expansion ...](docs/parameter_pack_expansion_(...).md)
149149
* [Register Keyword](docs/register.md)
150-
* [Regex](src/regex_mathch_search.cpp)
150+
* [Regex](docs/regex.md)
151151
* [Pseudo-random Number Generation, Distributions](docs/random_number.md)
152152
* [Raw Pointers, Smart (Shared, Unique, Weak) Pointers, Reference, addressof, reference_wrapper, std::ref](docs/pointers.md)
153153
* [Passing/ Returning Smart Pointers To/ From Functions](docs/passing_returning_smart_pointers_to_from_functions.md)
@@ -168,7 +168,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
168168
* [Temporary Objects](docs/temporary_objects.md)
169169
* [Track memory allocations(overriding new operator)](docs/track_memory_allocations_overriding_new_operator.md)
170170
* [Trivial, standard-layout, Passive/ Plain old data structure (PDS)/ (POD), and literal types](docs/trivial_standard_pds_pod_layout.md)
171-
* [Type Traits](src/type_traits.cpp)
171+
* [Type Traits](docs/type_traits.md)
172172
* [Typedef, Type alias (using keyword))](src/typedef.cpp)
173173
* [type_dispatch, integral_constant, true/false type](src/type_dispatch_integral_constant_true_false_type.cpp)
174174
* [Unions](docs/union.md)
@@ -195,7 +195,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
195195
* [Private Public Protected Inheritance](src/class/private_public_protected_inheritance.cpp)
196196
* [Protected Friend Class Function](src/class/protected_friend_class_function.cpp)
197197
* [Shadowing](src/class/shadowing.cpp)
198-
* [Static Member Function, Static Initialization Order Fiasco](src/class/static_member_function.cpp)
198+
* [Static Member Function, Static Initialization Order Fiasco](docs/static_member_function_order_fiasco.md)
199199
* [Special Member Functions](docs/class_special_member_functions.md)
200200
* [Virtual Function Abstract Class](docs/virtual_function_abstract_class.md)
201201
* [Virtual Destructor, Virtual Constructor](docs/virtual_destructor_virtual_constructor.md)
@@ -225,30 +225,29 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
225225

226226
## [Advance C++ Concepts and Idioms](#)
227227

228-
* [Argument-dependent lookup](src/argument_dependent_lookup.cpp)
228+
* [Argument-dependent lookup](docs/argument_dependent_lookup.md)
229229
* [Buffer overflow](src/buffer_overflow.cpp)
230230
* [Copy and Swap](docs/copy-and-swap_idiom.md)
231231
* [Generating and Debugging Dump File](docs/generating_and_debugging_dump_file.md)
232232
* [Static Casting, Dynamic Casting](src/cast.cpp)
233-
* [Run-Time Type Information(RTTI)](src/RTTI.cpp)
233+
* [Run-Time Type Information(RTTI)](docs/RTTI.md)
234234
* [Curiously Recurring Template Pattern (CRTP)](src/CRTP.cpp)
235-
* [Copy Elision, Return value optimization (RVO,NRVO)](src/RVO_NRVO_copy_elision.cpp)
236-
* [Double Dispatch](src/)
235+
* [Copy Elision, Return value optimization (RVO,NRVO)](docs/copy_elision.md)
236+
* [Double Dispatch](docs/double_dispatch.md)
237237
* [Most vexing parse](src/most_vexing_parse.cpp)
238238
* [Metaprogramming](docs/metaprogramming.md)
239239
* [Pointer to implementation (PIMPL)](src/pimpl/)
240-
* [Return Type Resolver](src/return_type_resolver.cpp)
240+
* [Return Type Resolver](docs/return_type_resolver.md)
241241
* [Resource Acquisition Is Initialization (RAII)](doc/RAII.md)
242242
* [Rule of 5](docs/rule_of_5.md)
243243
* [Substitution failure is not an error (SFINAE)](src/SFINAE.cpp)
244244
* [Stack overflow](docs/stack_overflow.md)
245245
* [Stack Unwinding](src/stack_unwinding.cpp)
246246
* [Type Erasure](docs/type_erasure.md)
247-
* [Template Specialization, Tag Dispatch](src/template_specialization_tag_dispatch.cpp)
247+
* [Template Specialization, Tag Dispatch](docs/template_specialization.md)
248248
* [Virtual Method Table(VTABLE), Virtual Table Pointer(VPTR)](docs/VTABLE_and_VPTR.md)
249249
* [Virtual address space](docs/virtual_address_space.md)
250250
* [Tricky Questions](src/tricky_questions.cpp)
251-
* [Type Erasure](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Type_Erasure)
252251
* [C++ Translation Units](docs/translation_units.md)
253252
* [Undefined behavior, Unspecified and Implementation-Defined](doc/undefined_unspecified_implementation_defined.md)
254253

docs/RTTI.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Run-Time Type Information (RTTI)
2+
Run-Time Type Information (RTTI) is a feature provided by some programming languages, most notably C++, that allows the type of an object to be determined during program execution. RTTI is useful in situations where the type of an object is not known at compile time and needs to be determined at run time. Here's a brief overview of its key aspects:
3+
4+
1. **Dynamic Type Identification**: RTTI allows a program to identify the actual type of object pointed to by a base pointer. This is particularly useful in the context of inheritance and polymorphism, where a base class pointer might actually refer to an object of a derived class.
5+
6+
2. **`typeid` Operator**: In C++, RTTI is often used via the `typeid` operator. This operator can be applied to objects, and it returns a reference to a `type_info` object that represents the type of the object. For example, `typeid(x)` returns the `type_info` of `x`.
7+
8+
3. **`dynamic_cast` Operator**: Another key component of RTTI is the `dynamic_cast` operator. It is used for safely converting pointers and references within an inheritance hierarchy. Unlike `static_cast`, `dynamic_cast` performs type checking at runtime and ensures that the cast is safe. If the cast is not possible, `dynamic_cast` returns a null pointer when used with pointers, or throws a `bad_cast` exception when used with references.
9+
10+
4. **Use Cases and Limitations**:
11+
- **Use Cases**: RTTI is useful in implementations that require knowledge of object types at runtime, like in designing generic containers, serialization/deserialization, and implementing dynamic polymorphism beyond what is achievable with virtual functions.
12+
- **Limitations**: The use of RTTI can lead to a slight overhead in terms of performance and memory usage. Some programming environments allow RTTI to be disabled to optimize performance.
13+
14+
5. **Controversy and Alternatives**: While RTTI provides dynamic type information capabilities, its use is sometimes debated among developers. Some argue that excessive use of RTTI may indicate a design problem in the application. Alternatives like visitor patterns, type tags, and dynamic interfaces are often used in place of RTTI to achieve similar functionality with potentially less overhead or more controlled type-handling mechanisms.
15+
16+
Certainly! A common scenario where RTTI is useful involves polymorphic class hierarchies, where you need to perform specific operations based on the actual type of the object at runtime. Let's consider a classic example in C++ involving a base class and multiple derived classes.
17+
18+
### Scenario:
19+
Imagine we have a class hierarchy for different types of media content, like `Audio`, `Video`, and `Image`, all derived from a base class `Media`. We have a function that takes a pointer to `Media` and needs to perform certain actions depending on whether the actual object is `Audio`, `Video`, or `Image`.
20+
21+
### Example Code:
22+
23+
```cpp
24+
#include <iostream>
25+
#include <typeinfo>
26+
27+
// Base class
28+
class Media {
29+
public:
30+
virtual ~Media() = default; // Virtual destructor for proper cleanup
31+
};
32+
33+
// Derived classes
34+
class Audio : public Media {};
35+
class Video : public Media {};
36+
class Image : public Media {};
37+
38+
// Function that uses RTTI
39+
void processMedia(Media* media) {
40+
if (typeid(*media) == typeid(Audio)) {
41+
std::cout << "Processing Audio content." << std::endl;
42+
// Specific audio processing
43+
} else if (typeid(*media) == typeid(Video)) {
44+
std::cout << "Processing Video content." << std::endl;
45+
// Specific video processing
46+
} else if (typeid(*media) == typeid(Image)) {
47+
std::cout << "Processing Image content." << std::endl;
48+
// Specific image processing
49+
} else {
50+
std::cout << "Unknown Media type." << std::endl;
51+
}
52+
}
53+
54+
int main() {
55+
Media* media1 = new Audio();
56+
Media* media2 = new Video();
57+
Media* media3 = new Image();
58+
59+
processMedia(media1);
60+
processMedia(media2);
61+
processMedia(media3);
62+
63+
delete media1;
64+
delete media2;
65+
delete media3;
66+
67+
return 0;
68+
}
69+
```
70+
71+
In this example:
72+
73+
- The `processMedia` function receives a pointer to a `Media` object.
74+
- It uses the `typeid` operator to determine the actual type of the object it received.
75+
- Based on the type, it performs type-specific processing (in this example, it just prints a message, but you can imagine more complex operations).
76+
- This allows us to write generic code that can handle different types of derived objects differently at runtime.
77+
78+
### Points to Note:
79+
- RTTI works only with polymorphic classes (those with at least one virtual function). Here, the virtual destructor in the `Media` class makes it polymorphic.
80+
- The `typeid` operator returns a reference to a `std::type_info` object that represents the type.
81+
- It's crucial to handle the memory correctly to avoid memory leaks (`delete` is used in this example).
82+
- While RTTI provides flexibility, it's advisable to use it judiciously, as over-reliance on type checking can lead to less maintainable code and could indicate potential design issues.
83+
84+
85+
86+
87+
Now you might ask the above example can be solved if we make a virtual function `processMedia` in the Media and implement it in the `Audio`, `Video`, and `Image`, can you make an example where we really need RTTI
88+
89+
Thats's correct that the previous example can be more elegantly solved using virtual functions. A scenario where RTTI really becomes necessary is when dealing with complex class hierarchies where the behavior cannot be resolved through virtual functions alone, or when dealing with third-party library classes where you can't modify the base class to add virtual functions. Let's consider a more nuanced example:
90+
91+
### Scenario:
92+
Suppose we have a graphical user interface (GUI) framework with a base class `Widget`. From `Widget`, we derive several classes like `Button`, `TextBox`, `Slider`, etc. Now, we have a function that needs to adjust the layout or perform specific actions depending on the type of widget, but without altering the base `Widget` class (perhaps because it's part of a third-party library).
93+
94+
### Example Code:
95+
96+
```cpp
97+
#include <iostream>
98+
#include <vector>
99+
#include <typeinfo>
100+
101+
// Base class
102+
class Widget {
103+
public:
104+
virtual ~Widget() = default;
105+
};
106+
107+
// Derived classes
108+
class Button : public Widget {};
109+
class TextBox : public Widget {};
110+
class Slider : public Widget {};
111+
112+
// Function to adjust layout based on widget type
113+
void adjustLayout(std::vector<Widget*>& widgets) {
114+
for (auto* widget : widgets) {
115+
if (typeid(*widget) == typeid(Button)) {
116+
std::cout << "Adjusting layout for Button." << std::endl;
117+
// Button-specific layout adjustments
118+
} else if (typeid(*widget) == typeid(TextBox)) {
119+
std::cout << "Adjusting layout for TextBox." << std::endl;
120+
// TextBox-specific layout adjustments
121+
} else if (typeid(*widget) == typeid(Slider)) {
122+
std::cout << "Adjusting layout for Slider." << std::endl;
123+
// Slider-specific layout adjustments
124+
} else {
125+
std::cout << "Unknown Widget type." << std::endl;
126+
}
127+
}
128+
}
129+
130+
int main() {
131+
std::vector<Widget*> widgets;
132+
133+
widgets.push_back(new Button());
134+
widgets.push_back(new TextBox());
135+
widgets.push_back(new Slider());
136+
137+
adjustLayout(widgets);
138+
139+
for (auto* widget : widgets) {
140+
delete widget;
141+
}
142+
143+
return 0;
144+
}
145+
```
146+
147+
In this example:
148+
149+
- The `adjustLayout` function needs to make specific adjustments based on the type of widget. This could be a scenario where the base class `Widget` cannot be modified to include virtual functions for layout adjustment.
150+
- The function iterates over a collection of widgets and uses RTTI to determine each widget's type.
151+
- Depending on the type, it performs specific actions (e.g., layout adjustments).
152+
153+
### Points to Note:
154+
- RTTI is particularly useful in scenarios where the class hierarchy is complex, and the behavior depends on more than just a single method call.
155+
- This kind of usage is common in frameworks or systems where you're extending or interacting with a codebase that you cannot modify (like third-party libraries).
156+
- The example demonstrates a situation where the behavior cannot be encapsulated within virtual functions, necessitating RTTI for type-specific behavior.
157+
158+

docs/aggregate-copy-default-direct-value-zero_initialization.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,146 @@ Certainly! Initialization in C++ is a fundamental concept, involving various met
4747

4848
Each of these initialization types is useful in different scenarios. The choice depends on factors like the type of object being initialized, efficiency considerations, readability of the code, and the version of the C++ standard being used. Uniform Initialization in C++11 and later offers a more consistent and safer approach across different contexts, reducing the complexity of choosing the appropriate initialization form.
4949

50+
# Examples
51+
52+
Certainly! Here are small examples for each type of initialization in C++:
53+
54+
1. **Initialization Lists (Constructor Initialization Lists)**:
55+
56+
```cpp
57+
#include <iostream>
58+
59+
class MyClass {
60+
public:
61+
MyClass(int a, int b) : x(a), y(b) {}
62+
void print() {
63+
std::cout << "x: " << x << ", y: " << y << std::endl;
64+
}
65+
private:
66+
int x;
67+
int y;
68+
};
69+
70+
int main() {
71+
MyClass obj(10, 20);
72+
obj.print();
73+
return 0;
74+
}
75+
```
76+
77+
2. **Aggregate Initialization**:
78+
79+
```cpp
80+
#include <iostream>
81+
82+
struct Point {
83+
int x;
84+
int y;
85+
};
86+
87+
int main() {
88+
Point p = {5, 7};
89+
std::cout << "x: " << p.x << ", y: " << p.y << std::endl;
90+
return 0;
91+
}
92+
```
93+
94+
3. **Copy Initialization**:
95+
96+
```cpp
97+
#include <iostream>
98+
99+
int main() {
100+
int a = 5;
101+
int b = a; // Copy initialization
102+
std::cout << "a: " << a << ", b: " << b << std::endl;
103+
return 0;
104+
}
105+
```
106+
107+
4. **Default Initialization**:
108+
109+
```cpp
110+
#include <iostream>
111+
112+
int main() {
113+
int a; // Default initialization (value is indeterminate)
114+
std::cout << "a: " << a << std::endl;
115+
return 0;
116+
}
117+
```
118+
119+
5. **Value Initialization**:
120+
121+
```cpp
122+
#include <iostream>
123+
124+
int main() {
125+
int a = int(); // Value initialization (sets to zero)
126+
std::cout << "a: " << a << std::endl;
127+
return 0;
128+
}
129+
```
130+
131+
6. **Uniform Initialization (List Initialization)**:
132+
133+
```cpp
134+
#include <iostream>
135+
136+
int main() {
137+
int a{42}; // Uniform initialization
138+
std::cout << "a: " << a << std::endl;
139+
return 0;
140+
}
141+
```
142+
143+
7. **Direct Initialization**:
144+
145+
```cpp
146+
#include <iostream>
147+
148+
int main() {
149+
int a(5); // Direct initialization
150+
std::cout << "a: " << a << std::endl;
151+
return 0;
152+
}
153+
```
154+
155+
8. **Zero Initialization**:
156+
157+
```cpp
158+
#include <iostream>
159+
160+
int main() {
161+
int a = {}; // Zero initialization (sets to zero)
162+
std::cout << "a: " << a << std::endl;
163+
return 0;
164+
}
165+
```
166+
167+
9. **Brace-Or-Equal Initializers (In-Class Initialization)**:
168+
169+
```cpp
170+
#include <iostream>
171+
172+
class MyClass {
173+
public:
174+
int x = 10; // In-Class Initialization
175+
void print() {
176+
std::cout << "x: " << x << std::endl;
177+
}
178+
};
179+
180+
int main() {
181+
MyClass obj;
182+
obj.print();
183+
return 0;
184+
}
185+
```
186+
187+
These examples illustrate the use of each initialization type in different contexts.
188+
189+
50190
# Initialization Lists
51191
Using Initialization lists to initialize Fields
52192

0 commit comments

Comments
 (0)