I know that compilers have much freedom in implementing std::type_info
functions\' behavior.
I\'m thinking about using it to compare object types, so I\'d l
The current answers for questions 1 and 2 are perfectly correct, and they're essentially just details for the type_info class - no point in repeating those answers.
For questions 3 and 4, it's important to understand what precisely is a type in C++, and how they relate to names. For starters, there are a whole bunch of predefined types, and those have names: int, float, double
. Next, there are some constructed types that do not have names of their own: const int, int*, const int*, int* const
. There are function types int (int)
and function pointer types int (*)(int)
.
It's sometimes useful to give a name to an unnamed type, which is possible using typedef
. For instance, typedef int* pint
or typedef int (*pf)(int);
. This introduces a name, not a new type.
Next are user-defined types: structs, classes, unions. There's a good convention to give them names, but it's not mandatory. Don't add such a name with typedef, you can do so directly: struct Foo { };
instead of typedef struct {} Foo;
. It's common to have class definitions in headers, which end up\ in multiple translation units. That does mean the class is defined more than once. This is still the same type, and therefore you aren't allowed to play tricks with macros to change the class member definitions.
A template class is not a type, it's a recipe for types. Two instantiations of a single class template are distinct types if the template arguments are different types (or values). This works recursively: Given template
, Foo
is the same type as Foo
if and only if Bar
is another name for the type int
.