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
@@ -29,7 +29,7 @@ The following code contains an example of a failed **dynamic_cast** that throws
29
29
```cpp
30
30
// expre_bad_cast_Exception.cpp
31
31
// compile with: /EHsc /GR
32
-
#include <typeinfo.h>
32
+
#include <typeinfo>
33
33
#include <iostream>
34
34
35
35
class Shape {
@@ -55,7 +55,7 @@ int main() {
55
55
}
56
56
```
57
57
58
-
The exception is thrown because the object being cast (a Shape) is not derived from the specified cast type (Circle). To avoid the exception, add these declarations to `main`:
58
+
The exception is thrown because the object being cast (a Shape) isn't derived from the specified cast type (Circle). To avoid the exception, add these declarations to `main`:
Copy file name to clipboardExpand all lines: docs/cpp/typeid-operator.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: "typeid Operator"
3
-
ms.date: "11/04/2016"
3
+
ms.date: "10/04/2019"
4
4
helpviewer_keywords: ["typeid operator"]
5
5
ms.assetid: 8871cee6-d6b9-4301-a5cb-bf3dc9798d61
6
6
---
@@ -17,25 +17,25 @@ typeid(expression)
17
17
18
18
The **typeid** operator allows the type of an object to be determined at run time.
19
19
20
-
The result of **typeid** is a `const type_info&`. The value is a reference to a `type_info` object that represents either the *type-id* or the type of the *expression*, depending on which form of **typeid** is used. See [type_info Class](../cpp/type-info-class.md) for more information.
20
+
The result of **typeid** is a `const type_info&`. The value is a reference to a `type_info` object that represents either the *type-id* or the type of the *expression*, depending on which form of **typeid** is used. For more information, see [type_info Class](../cpp/type-info-class.md).
21
21
22
-
The **typeid** operator does not work with managed types (abstract declarators or instances), see [typeid](../extensions/typeid-cpp-component-extensions.md) for information on getting the <xref:System.Type> of a specified type.
22
+
The **typeid** operator doesn't work with managed types (abstract declarators or instances). For information on getting the <xref:System.Type> of a specified type, see [typeid](../extensions/typeid-cpp-component-extensions.md).
23
23
24
-
The **typeid** operator does a run-time check when applied to an l-value of a polymorphic class type, where the true type of the object cannot be determined by the static information provided. Such cases are:
24
+
The **typeid** operator does a run-time check when applied to an l-value of a polymorphic class type, where the true type of the object can't be determined by the static information provided. Such cases are:
25
25
26
26
- A reference to a class
27
27
28
-
- A pointer, dereferenced with \*
28
+
- A pointer, dereferenced with `*`
29
29
30
-
- A subscripted pointer (i.e. []). (Note that it is generally not safe to use a subscript with a pointer to a polymorphic type.)
30
+
- A subscripted pointer (`[ ]`). (It's not safe to use a subscript with a pointer to a polymorphic type.)
31
31
32
-
If the *expression* points to a base class type, yet the object is actually of a type derived from that base class, a `type_info` reference for the derived class is the result. The *expression* must point to a polymorphic type (a class with virtual functions). Otherwise, the result is the `type_info` for the static class referred to in the *expression*. Further, the pointer must be dereferenced so that the object it points to is used. Without dereferencing the pointer, the result will be the `type_info` for the pointer, not what it points to. For example:
32
+
If the *expression* points to a base class type, yet the object is actually of a type derived from that base class, a `type_info` reference for the derived class is the result. The *expression* must point to a polymorphic type (a class with virtual functions). Otherwise, the result is the `type_info` for the static class referred to in the *expression*. Further, the pointer must be dereferenced so that the object used is the one it points to. Without dereferencing the pointer, the result will be the `type_info` for the pointer, not what it points to. For example:
33
33
34
34
```cpp
35
35
// expre_typeid_Operator.cpp
36
36
// compile with: /GR /EHsc
37
37
#include<iostream>
38
-
#include<typeinfo.h>
38
+
#include<typeinfo>
39
39
40
40
classBase {
41
41
public:
@@ -56,9 +56,9 @@ int main() {
56
56
}
57
57
```
58
58
59
-
If the *expression* is dereferencing a pointer, and that pointer's value is zero, **typeid** throws a [bad_typeid exception](../cpp/bad-typeid-exception.md). If the pointer does not point to a valid object, a `__non_rtti_object` exception is thrown, indicating an attempt to analyze the RTTI that triggered a fault (like access violation), because the object is somehow invalid (bad pointer or the code wasn't compiled with [/GR](../build/reference/gr-enable-run-time-type-information.md)).
59
+
If the *expression* is dereferencing a pointer, and that pointer's value is zero, **typeid** throws a [bad_typeid exception](../cpp/bad-typeid-exception.md). If the pointer doesn't point to a valid object, a `__non_rtti_object` exception is thrown. It indicates an attempt to analyze the RTTI that triggered a fault because the object is somehow invalid. (For example, it's a bad pointer, or the code wasn't compiled with [/GR](../build/reference/gr-enable-run-time-type-information.md)).
60
60
61
-
If the *expression* is neither a pointer nor a reference to a base class of the object, the result is a `type_info` reference representing the static type of the *expression*. The *static type* of an expression refers to the type of an expression as it is known at compile time. Execution semantics are ignored when evaluating the static type of an expression. Furthermore, references are ignored when possible when determining the static type of an expression:
61
+
If the *expression* is not a pointer, and not a reference to a base class of the object, the result is a `type_info` reference representing the static type of the *expression*. The *static type* of an expression refers to the type of an expression as it is known at compile time. Execution semantics are ignored when evaluating the static type of an expression. Furthermore, references are ignored when possible when determining the static type of an expression:
62
62
63
63
```cpp
64
64
// expre_typeid_Operator_2.cpp
@@ -85,5 +85,5 @@ T max( T arg1, T arg2 ) {
85
85
86
86
## See also
87
87
88
-
[Run-Time Type Information](../cpp/run-time-type-information.md)<br/>
89
-
[Keywords](../cpp/keywords-cpp.md)
88
+
[Run-Time Type Information](../cpp/run-time-type-information.md)\
To avoid the errors in the previous example, use **bool** instead of **BOOL** consistently in both declarations of `f`.
453
453
454
+
### Standard Library improvements
455
+
456
+
The non-standard headers \<stdexcpt.h> and \<typeinfo.h> have been removed. Code that includes them should instead include the standard headers \<exception> and \<typeinfo>, respectively.
457
+
454
458
## <a name="update_160"></a> Bug fixes and behavior changes in Visual Studio 2019
455
459
456
460
### Reinterpret_cast in a constexpr function
@@ -716,7 +720,7 @@ The unordered container `reserve` function now actually reserves for N elements,
716
720
717
721
- Previously, some time values that were passed to the concurrency library would overflow, for example, `condition_variable::wait_for(seconds::max())`. Now fixed, the overflows changed behavior on a seemingly random 29-day cycle (when uint32_t milliseconds accepted by underlying Win32 APIs overflowed).
718
722
719
-
- The <ctime> header now correctly declares `timespec` and `timespec_get` in namespace `std` in addition to declaring them in the global namespace.
723
+
- The \<ctime> header now correctly declares `timespec` and `timespec_get` in namespace `std` in addition to declaring them in the global namespace.
0 commit comments