Home > Software design >  uniqueness of pointer to type_info object
uniqueness of pointer to type_info object

Time:12-26

I was looking through the object factory chapter in the book referenced here. The TypeInfo class above which is intended to be unique for each C class type uses a pointer to std::type_info under the hood. I'm wondering if this is guaranteed by the latest standards to be unique for a given type?

If that is not guaranteed to be unique is there an alternate implementation of TypeInfo that can achieve such a guarantee?

CodePudding user response:

I do not see anything in the current standard draft making a guarantee that there will be only one std::type_info object referenced by typeid expressions for the same type, nor do I see any in C 98.

But I also don't see how the linked code would care about that.

CodePudding user response:

I'm not sure about uniqueness of std::std::type_info pointers, but there are std::type_index (https://en.cppreference.com/w/cpp/types/type_index) type which is "a wrapper class around a std::type_info object, that can be used as index in associative and unordered associative containers". So I read this as this index is unique for each given type.

  •  Tags:  
  • c
  • Related