Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C++ is a statically typed language, thus types only really exist in a meaningful way at the compile time, not at runtime. At runtime the best C++ can give you is RTTI which provides you with things like dynamic_cast&lt;> and typeid(), which are however limited to giving you information along the inheritance hierarchy only, i.e. if you have</p> <pre><code>class Base class DerivedA : public Base class DerivedB : public Base </code></pre> <p>and you have a <code>Base*</code> or <code>Base&amp;</code> you can find out if it's a <code>Base</code>, a <code>DerivedA</code> or a <code>DerivedB</code>. However in your case you only have a <code>void*</code>, which is completely outside of any inheritance hierarchy and thus has no type information associated with it. Thus all typeid() will tell you is that you have a <code>void*</code>, it won't tell you whatever type might hide behind it.</p> <p>Furthermore a construct like:</p> <pre><code>print &lt;typeid(myvector.at(2))&gt; (myvector.at(2)); </code></pre> <p>wouldn't work in C++ either, as the type for a template also needs to be known at compile time. Here however the type of <code>.at(2)</code> would only be known at runtime.</p> <p>So to solve your problem you have to do the type handling yourself. Meaning you have to store the type along with the object you want to store, which would look something like this:</p> <pre><code>struct Value { enum { kInt, kString } type; union { int vInt; char* vChar; } value; }; [...] Value v; v.type = Value::kInt; v.value.vInt = 5; switch(v.type) { case Value::kInt: // do int specific stuff break; case Value::kString: // do string specific stuff break; } </code></pre> <p>The <code>boost::variant&lt;&gt;</code> class that visitor mentioned provides basically the above in a nicely packaged way.</p> <p>Another thing worth to mention is <code>decltype</code>, <code>decltype</code> is new in C++11 standard and allows you to get the actual type of an object and thus you can write code like:</p> <pre><code>int a; decltype(a) b; </code></pre> <p>Where <code>b</code> gets the same type as <code>a</code>, i.e. <code>int</code>. This sounds exactly like what you want, but it is not, <code>decltype()</code> has the same restrictions as before. It can only work when the type is already known at compile time, it can't do anything with types only known at runtime. Thus it will not work in your situation and is only really useful when doing some more complex template programming.</p> <p>Long story short, use <code>boost::variant&lt;&gt;</code> or write yourself a class that works in a similar way.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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