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
After a quick look at the documentation, I would say that :
std::type_info::name always returns two different strings for two different types, otherwise it means that the compiler lost itself while resolving types and you shouldn't use it anymore.
Reference tells : "before returns true if the type precedes the type of rhs in the collation order. The collation order is just an internal order kept by a particular implementation and is not necessarily related to inheritance relations or declaring order." You therefore have the guarantee that no types has the same rank in the collation order.
Each instantiation of a template class is a different type. Specialization make no exceptions.
I don't really understand what you mean. If you mean something like having typedef foo bar;
in two separate compilation units and that bar is the same in both, it works that way. If you mean typedef foo bar; typedef int bar;
, it doesn't work (except if foo is int).
About your other questions :
operator==
instead of make it virtual and override it.Type_info is implementation defined so I really wouldn't rely on it. However, based on my experiences using g++ and MSVC, assumptions 1,3 and 4 hold... not really sure about #2.
Is there any reason you can't use another method like this?
template<typename T, typename U>
struct is_same { static bool const result = false; };
template<typename T>
struct is_same<T, T> { static bool const result = true; };
template<typename S, typename T>
bool IsSame(const S& s, const T& t) { return is_same<S,T>::result; }
Standard 18.5.1 (Class type_info) :
The class type_info describes type information generated by the implementation. 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. The names, encoding rule, and collating sequence for types are all unspecified and may differ between programs.
From my understanding :
std:type_info::name
. The standard only states that name
returns an implementation-defined NTBS, and I believe a conforming implementation could very well return the same string for every type.Regarding the second set of questions :
type_info
. Andrei Alexandrescu proposes a TypeInfo
wrapper in its Modern C++ Design book. Note that the objects returned by typeid
have static storage so you can safely store pointers without worrying about object lifetimetype_info
comparison are extremely efficient (there really isn't much to compare).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 <typename T> struct Foo{};
, Foo<Foo<int> >
is the same type as Foo<Foo<Bar> >
if and only if Bar
is another name for the type int
.
You can store it like this.
class my_type_info
{
public:
my_type_info(const std::type_info& info) : info_(&info){}
std::type_info get() const { return *info_;}
private:
const std::type_info* info_;
};
EDIT:
C++ standard 5.2.8.
The result of a typeid expression is an lvalue of static type const std::type_info...
Which means you can use it like this.
my_type_info(typeid(my_type));
The typeid function returns an lvalue (it is not temporary) and therefore the address of the returned type_info is always valid.