You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/RTTI.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Run-Time Type Information (RTTI) is a feature provided by some programming langu
13
13
14
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
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.
16
+
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
17
18
18
### Scenario:
19
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`.
SFINAE, which stands for "Substitution Failure Is Not An Error," is a concept in C++ template programming. It's a rule that applies during the template instantiation process. The idea is that if a substitution of template parameters into a template results in an invalid code, this is not in itself an error. Instead, the invalid template is simply discarded from the set of potential templates to use.
3
+
4
+
This concept is particularly useful for creating template specializations that are only valid for certain types or conditions, enabling more flexible and powerful template designs.
In this example, there are two versions of the `isIntegral` function template. The first version is enabled (via `std::enable_if`) only for integral types, while the second version is enabled for non-integral types. When you call `isIntegral` with an integer, the first version is instantiated. If you call it with a non-integer, the second version is instantiated.
36
+
37
+
### Explanation
38
+
39
+
- `std::enable_if`: This is a standard library utility that provides a member typedef `type` if the given boolean constant is true. If the boolean is false, there's no member typedef. This utility is commonly used for controlling template instantiation.
40
+
41
+
- `std::is_integral`: This is a type trait that checks if a type is an integral type (like int, char, etc.).
42
+
43
+
### How SFINAE Comes Into Play
44
+
45
+
In the example, if `T` is an integral type, the `std::enable_if<std::is_integral<T>::value, bool>::type` becomes `bool`, so the first `isIntegral` function template is valid. If `T` is not an integral type, this substitution fails, but due to SFINAE, this failure is not an error; instead, the first `isIntegral` template is simply discarded, and the compiler looks for other templates (in this case, the second `isIntegral`).
46
+
47
+
SFINAE allows for powerful metaprogramming techniques in C++, enabling the creation of templates that can adapt to different types and conditions at compile time, leading to more efficient and tailored code.
0 commit comments