Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Where did you get the idea that "string comparison has to be performed"? In order to determine if two <code>type_info</code> objects designate the same type, you need to compare these <code>type_info</code> objects directly, as in <code>typeid(obj) == typeid(Test)</code>. </p> <p>In fact, you <em>cannot</em> do the same tings by comparing the strings returned by <code>type_info::name()</code> member simply because the language makes no guarantees about these strings at all. More specifically, it makes no guarantees about the uniqueness of these strings for each given type. They all can return <code>"Hello World!"</code> for all types. Or they can return an empty string for all types. Usually the implementations behave nicer than that, but in any case the <code>name()</code> member is there for some potential debugging/informational purposes. You cannot meaningfully rely on <code>type_info::name()</code> in the actual functionality of your code.</p> <p>Also, the language standard says that <code>type_info</code> objects are lvalue objects with static storage duration. I'd expect these <code>type_info</code> object to maintain their "address identity" for each specific type (although I'm not sure the standard actually guarantees that). I.e. I'd expect that <code>&amp;typeid(type) == &amp;typeid(type)</code> is always true (i.e. every time you invoke <code>typeid</code> for the same type, you get the same lvalue as the result). If so, you can compare the <em>addresses</em> of <code>type_info</code> objects instead of comparing them using the <code>==</code> operator. You can also use the addresses to build some more complicated data structure for type matching, like an associative array. You can't use <code>switch/case</code> with it though. (And again, I'm not sure my assumption about stable address identity of <code>type_info</code> is valid). In fact it is not. As Johannes Schaub noted in the comments, the correct way to put <code>type_info</code>s into an ordered container is to use the <code>type_info::before()</code> to establish ordering.</p> <p>Of course, in your specific case using a manually implemented integer identifier for the class (if you really really need to go that way) might be much more efficient. It also allows you a greater flexibility to implement your intent better. For example, <code>typeid(Test) == typeid(const Test)</code> will evaluate to <code>false</code>, which might not be what you want. However, highjacking the conversion operator to such a basic type as <code>int</code> for that purpose is definitely not a good idea. If you really need it, make it a named method and return something named, not a "magic constant".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload