When we use typeid i.e typeid(variable).name() Does it give out string as the output because if it does it could be helpful in comparisons with strings.
According to the standard, it is an implementation-defined null-terminated const char*
:
18.7.1 Class type_info
....
const char* name() const noexcept;
Returns: An implementation-defined
NTBS
.Remarks: The message may be a null-terminated multibyte string (17.5.2.1.4.2), suitable for conversion and display as a
wstring
(21.3, 22.4.1.4)
Since the content is implementation-defined, it cannot be compared with other strings in a reliable way, unless we restrict ourselves to specific implementations.
typeid(variable).name()
returns a pointer a null terminated string, which can be compared using strcmp()
. However a better way to check a type of variable is
if (typeid(a) == typeid(int))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With