Typeid

Ok, so if I remember correctly, typeid returns some sort of char or maybe it’s a string. For now I’ll just assume that’s correct. If a make a new 'type' with typedef, will it have a different return than the type that it’s covering?

1
2
3
4
5
// ok’d how to use typedef or typeid, so bear with me. I apologize for any 
// mistakes in advance.

typedef xxT as int; // obviously not how it works but whatever.
std::cout << typeid(xxT) == typeid(int); // t or f? 
Ok, so if I remember correctly, typeid returns some sort of char or maybe it’s a string.

Perhaps. Or perhaps, if I read correctly, it returns a std::type_info:
https://en.cppreference.com/w/cpp/language/typeid

For now I’ll just assume that’s correct.

As you wish.

If a make a new 'type' with typedef, will it have a different return than the type that it’s covering?

1) If you make a new 'type' by means of typedef in C++, you are a magician:

https://en.cppreference.com/w/cpp/language/typedef
cppreference wrote:
The typedef specifier, when used in a declaration's decl-specifier-seq, […] declares typedef-names […] which is a synonym for the type of the object or function that it would become if the keyword typedef were removed.
[…]
The typedef-names are aliases for existing types, and are not declarations of new types. Typedef cannot be used to change the meaning of an existing type name (including a typedef-name). Once declared, a typedef-name may only be redeclared to refer to the same type again.


2) If you declare a new type, that doesn’t cover for any other type. It’s just a new type.
You can declare a new type for example by simply typing
 
class MyNewType {}; // here you are! 


1
2
typedef xxT as int; // obviously not how it works but whatever.
std::cout << typeid(xxT) == typeid(int); // t or f?  

Ehm… If you’ve already written the code, why don’t you simply try to execute it?

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <typeinfo>


int main()
{
    typedef int xxT;
    // Personal preference: I find it more legible: using xxT = int;
    std::cout << ( typeid(xxT) == typeid(int) ? "Bingo!" : ":-(" ) << '\n';
}


Output:
Bingo!

That actually cleared up a lot of stuff lol.
Thanks for putting up with my antics!
Topic archived. No new replies allowed.