Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Macros are expanded before any code is compiled, so unfortunately they don't have any concept of types.</p> <p>Depending on your needs, you might be able to use a traits class instead though. It's theoretically a bit more efficient and portable than RTTI and typeid, but it only works for types you've explicitly told it about. For example:</p> <pre><code>template &lt;typename T&gt; struct Traits { static const char * TYPE_NAME; }; // Generic definition as a fall-back: template &lt;typename T&gt; const char * Traits&lt;T&gt;::TYPE_NAME = "unknown"; // Explicit definitions template &lt; &gt; const char * Traits&lt; int &gt;::TYPE_NAME = "int"; template &lt; &gt; const char * Traits&lt; float &gt;::TYPE_NAME = "float"; template &lt; &gt; const char * Traits&lt; ExampleClass &gt;::TYPE_NAME = "ExampleClass"; </code></pre> <p>The explicit definitions are a bit cumbersome, so you could create a macro to make them more readable:</p> <pre><code>#define DECLARE_TYPE_TRAIT(name) template &lt; &gt; const char * Traits&lt;name&gt;::TYPE_NAME = #name; DECLARE_TYPE_TRAITS(int) DECLARE_TYPE_TRAITS(float) DECLARE_TYPE_TRAITS(ExampleClass) </code></pre> <p>Using the traits class in your code is really easy. You just instantiate the traits template on whatever type you want to look up, and access the TYPE_NAME member:</p> <pre><code>int foo; ExampleClass blah; cout &lt;&lt; Traits&lt;decltype(foo)&gt;::TYPE_NAME &lt;&lt; endl; cout &lt;&lt; Traits&lt;decltype(blah)&gt;::TYPE_NAME &lt;&lt; endl; </code></pre>
    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.
    2. 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