Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Does typeid return string type?

Tags:

c++

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.

like image 653
x86-64 Avatar asked Mar 15 '23 00:03

x86-64


2 Answers

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.

like image 156
AlexD Avatar answered Mar 27 '23 18:03

AlexD


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)) 
like image 27
Andrej Adamenko Avatar answered Mar 27 '23 19:03

Andrej Adamenko