Files
2025-10-25 03:02:53 +03:00

299 lines
11 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[support.rtti]
# 17 Language support library [[support]](./#support)
## 17.7 Type identification [support.rtti]
### [17.7.1](#general) General [[support.rtti.general]](support.rtti.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3264)
The header [<typeinfo>](#header:%3ctypeinfo%3e "17.7.2Header <typeinfo> synopsis[typeinfo.syn]") defines a
type associated with type information generated by the implementation[.](#general-1.sentence-1)
It also defines two types for reporting dynamic type identification errors[.](#general-1.sentence-2)
The header [<typeindex>](#header:%3ctypeindex%3e "17.7.6Header <typeindex> synopsis[type.index.synopsis]") defines
a wrapper type for use as an index type in associative containers ([[associative]](associative "23.4Associative containers"))
and in unordered associative containers ([[unord]](unord "23.5Unordered associative containers"))[.](#general-1.sentence-3)
### [17.7.2](#typeinfo.syn) Header <typeinfo> synopsis [[typeinfo.syn]](typeinfo.syn)
[🔗](#header:%3ctypeinfo%3e)
// all freestandingnamespace std {class type_info; class bad_cast; class bad_typeid;}
### [17.7.3](#type.info) Class type_info [[type.info]](type.info)
[🔗](#lib:type_info_)
namespace std {class type_info {public:virtual ~type_info(); constexpr bool operator==(const type_info& rhs) const noexcept; bool before(const type_info& rhs) const noexcept;
size_t hash_code() const noexcept; const char* name() const noexcept;
type_info(const type_info&) = delete;
type_info& operator=(const type_info&) = delete; };}
[1](#type.info-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3306)
The classtype_info describes type information generated by the implementation ([[expr.typeid]](expr.typeid "7.6.1.8Type identification"))[.](#type.info-1.sentence-1)
Objects of this class effectively store a pointer to a name for the type, and
an encoded value suitable for comparing two types for equality or collating order[.](#type.info-1.sentence-2)
The names, encoding rule, and collating sequence for types are all unspecifiedand may differ between programs[.](#type.info-1.sentence-3)
[🔗](#lib:operator==,type_info)
`constexpr bool operator==(const type_info& rhs) const noexcept;
`
[2](#type.info-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3322)
*Effects*: Compares the current object with rhs[.](#type.info-2.sentence-1)
[3](#type.info-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3326)
*Returns*: true if the two values describe the same type[.](#type.info-3.sentence-1)
[🔗](#lib:before,type_info)
`bool before(const type_info& rhs) const noexcept;
`
[4](#type.info-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3338)
*Effects*: Compares the current object with rhs[.](#type.info-4.sentence-1)
[5](#type.info-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3342)
*Returns*: true if*this precedes rhs in the implementation's collation order[.](#type.info-5.sentence-1)
[🔗](#lib:hash_code,type_info)
`size_t hash_code() const noexcept;
`
[6](#type.info-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3356)
*Returns*: An unspecified value, except that within a single execution of the
program, it shall return the same value for any two type_info objects which compare equal[.](#type.info-6.sentence-1)
[7](#type.info-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3362)
*Remarks*: An implementation should return different values for twotype_info objects which do not compare equal[.](#type.info-7.sentence-1)
[🔗](#lib:name,type_info)
`const char* name() const noexcept;
`
[8](#type.info-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3375)
*Returns*: An implementation-defined ntbs[.](#type.info-8.sentence-1)
[9](#type.info-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3379)
*Remarks*: The message may be a[null-terminated multibyte string](multibyte.strings#def:ntmbs "16.3.3.3.4.3Multibyte strings[multibyte.strings]"),
suitable for conversion and display as awstring ([[string.classes]](string.classes "27.4String classes"), [[locale.codecvt]](locale.codecvt "28.3.4.2.5Class template codecvt"))[.](#type.info-9.sentence-1)
### [17.7.4](#bad.cast) Class bad_cast [[bad.cast]](bad.cast)
[🔗](#lib:bad_cast_)
namespace std {class bad_cast : public exception {public:// see [[exception]](exception "17.9.3Class exception") for the specification of the special member functionsconstexpr const char* what() const noexcept override; };}
[1](#bad.cast-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3401)
The classbad_cast defines the type of objects thrown
as exceptions by the implementation to report the execution of an invaliddynamic_cast expression ([[expr.dynamic.cast]](expr.dynamic.cast "7.6.1.7Dynamic cast"))[.](#bad.cast-1.sentence-1)
[🔗](#lib:what,bad_cast)
`constexpr const char* what() const noexcept override;
`
[2](#bad.cast-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3416)
*Returns*: An implementation-defined ntbs[.](#bad.cast-2.sentence-1)
### [17.7.5](#bad.typeid) Class bad_typeid [[bad.typeid]](bad.typeid)
[🔗](#lib:bad_typeid_)
namespace std {class bad_typeid : public exception {public:// see [[exception]](exception "17.9.3Class exception") for the specification of the special member functionsconstexpr const char* what() const noexcept override; };}
[1](#bad.typeid-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3435)
The classbad_typeid defines the type of objects
thrown as exceptions by the implementation to report a null pointer
in atypeid expression ([[expr.typeid]](expr.typeid "7.6.1.8Type identification"))[.](#bad.typeid-1.sentence-1)
[🔗](#lib:what,bad_typeid)
`constexpr const char* what() const noexcept override;
`
[2](#bad.typeid-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3450)
*Returns*: An implementation-defined ntbs[.](#bad.typeid-2.sentence-1)
### [17.7.6](#type.index.synopsis) Header <typeindex> synopsis [[type.index.synopsis]](type.index.synopsis)
[🔗](#header:%3ctypeindex%3e)
#include <compare> // see [[compare.syn]](compare.syn "17.12.1Header <compare> synopsis")namespace std {class type_index; template<class T> struct hash; template<> struct hash<type_index>;}
### [17.7.7](#type.index) Class type_index [[type.index]](type.index)
[🔗](#lib:type_index)
namespace std {class type_index {public: type_index(const type_info& rhs) noexcept; bool operator==(const type_index& rhs) const noexcept; bool operator< (const type_index& rhs) const noexcept; bool operator> (const type_index& rhs) const noexcept; bool operator<=(const type_index& rhs) const noexcept; bool operator>=(const type_index& rhs) const noexcept;
strong_ordering operator<=>(const type_index& rhs) const noexcept;
size_t hash_code() const noexcept; const char* name() const noexcept; private:const type_info* target; // *exposition only*// Note that the use of a pointer here, rather than a reference,// means that the default copy/move constructor and assignment// operators will be provided and work as expected.};}
[1](#type.index-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3494)
The class type_index provides a simple wrapper fortype_info which can be used as an index type in associative
containers ([[associative]](associative "23.4Associative containers")) and in unordered associative
containers ([[unord]](unord "23.5Unordered associative containers"))[.](#type.index-1.sentence-1)
[🔗](#lib:type_index,constructor)
`type_index(const type_info& rhs) noexcept;
`
[2](#type.index-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3506)
*Effects*: Constructs a type_index object, the equivalent of target = &rhs[.](#type.index-2.sentence-1)
[🔗](#lib:operator==,type_index)
`bool operator==(const type_index& rhs) const noexcept;
`
[3](#type.index-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3517)
*Returns*: *target == *rhs.target[.](#type.index-3.sentence-1)
[🔗](#lib:operator%3c,type_index)
`bool operator<(const type_index& rhs) const noexcept;
`
[4](#type.index-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3528)
*Returns*: target->before(*rhs.target)[.](#type.index-4.sentence-1)
[🔗](#lib:operator%3e,type_index)
`bool operator>(const type_index& rhs) const noexcept;
`
[5](#type.index-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3539)
*Returns*: rhs.target->before(*target)[.](#type.index-5.sentence-1)
[🔗](#lib:operator%3c=,type_index)
`bool operator<=(const type_index& rhs) const noexcept;
`
[6](#type.index-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3550)
*Returns*: !rhs.target->before(*target)[.](#type.index-6.sentence-1)
[🔗](#lib:operator%3e=,type_index)
`bool operator>=(const type_index& rhs) const noexcept;
`
[7](#type.index-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3561)
*Returns*: !target->before(*rhs.target)[.](#type.index-7.sentence-1)
[🔗](#lib:operator%3c=%3e,type_index)
`strong_ordering operator<=>(const type_index& rhs) const noexcept;
`
[8](#type.index-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3572)
*Effects*: Equivalent to:if (*target == *rhs.target) return strong_ordering::equal;if (target->before(*rhs.target)) return strong_ordering::less;return strong_ordering::greater;
[🔗](#lib:hash_code,type_index)
`size_t hash_code() const noexcept;
`
[9](#type.index-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3588)
*Returns*: target->hash_code()[.](#type.index-9.sentence-1)
[🔗](#lib:name,type_index)
`const char* name() const noexcept;
`
[10](#type.index-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3599)
*Returns*: target->name()[.](#type.index-10.sentence-1)
[🔗](#lib:hash,type_index)
`template<> struct hash<type_index>;
`
[11](#type.index-11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L3610)
For an object index of type type_index,hash<type_index>()(index) shall evaluate to the same result as index.hash_code()[.](#type.index-11.sentence-1)