title: Using typeid for simple decisions
body: <p>My question concerns simple catching of casting problems at runtime in C++. I understand that C++ provides no 'RTTI' under most circumstances (let's just say I can't change my compiler settings and turn it on, for argument's sake.).</p>
  
  <p>In my project, I have a container provided to me by an ancient library. Let's call that Frodo and the container Bane.</p>
  
  <p>Frodo's Bane is a container I must interact with directly. It has been poorly implemented and contains several maps of the same information which I have to manage separately. The container contains multiple instances of a certain type, call it Ring.</p>
  
  <p>For my implementation, I have to use a subtype of Ring, the OneRing, for all other operations. This is due to the external requirements for my particular implementation of a Frodo's Bane.</p>
  
  <p>I must sometimes look through my lists and let's just say that there are things I need from a OneRing that I can't get from just any old Ring.</p>
  
  <p>There is a CHANCE, just an occasional CHANCE, that Frodo will put a normal old Ring into my Bane.</p>
  
  <p>Being far too Pepsi gen to have realised that C++ was incapable of answering a simple question like 'is this Ring also a OneRing' or to catch exceptions arising from mistaking a Ring for a OneRing, I did a bit of Googling and found that there are things like typeid and type_of, but that there are lots of reasons not to use each.</p>
  
  <p>So, limiting the problem right down to simply wanting to avoid mistaking a Ring for a OneRing, is there ANYTHING I can safely and efficiently do, WITHOUT being able to change the implementation of Ring.</p>
  
  <p>Thanks heaps for any advice!</p>
  
  <p><strong>UPDATE:</strong> Ok there is plenty of good advice below but after a day of experimentation I can say the following things about RTTI in native Visual Studio 2008 C++:</p>
  
  <ul>
  <li>C++ only has RTTI on <em>pointers</em> to <em>classes</em>. You can't dynamic cast a void* and if you ask for the type info it will be void*. The RTTI is 'on' the <em>pointer</em>. I understand why you don't 'cast' a member (from C++'s crazy viewpoint, in languages without members you don't have to make such an artificial distinction), and perhaps these facts are related for some archaic efficiency/compiler complexity reason.</li>
  <li>If you get the type info of a Ring* to a OneRing, it will be Ring*. Why they chose not to put the information on the <em>classes</em> I have no idea. This makes typeid() essentially useless - it even returns a private class with only one member you can access, a char* name! That's like 11/10 on the unfriendliness and uselessness scale - did they do it on purpose?</li>
  <li>You <em>can</em> cast down the inheritance tree safely with a dynamic cast but again you can't do this unless you have and know a common inheritance ancestor for any data you receive.</li>
  <li>If you fall outside the above scenario, or you don't have RTTI for whatever reason, you are up the proverbial creek, because there is no way to catch an exception or generate an error resulting from an incorrect casting unless you implement some form of RTTI yourself.</li>
  <li>Things like COM seek to give more useful RTTI by giving everything a common base that has some predeclared RTTI information on it... I think. :-)</li>
  </ul>
  
  <p>As a side note, Microsoft states that (at least since 2008) <strong>RTTI is a part of C++ and is on by default</strong>. It is <em>unbelievable</em> how much conflicting information on this alone I found.</p>
  
  <p>This may be old news to those of you who have spent most of your time in Native Land, but for me this took a session of experimenting, some Googling, some posting on StackOverflow, some more Googling and some more experimentation.</p>
  
  <p>Thanks to everyone who pointed me in the right direction, and please do correct me if you still feel I've missed something. :-)</p>