Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Anyway there will be at once one switch if you will not use run-time polymorphism. If you will use static polymorphism than you need either specialization or switch. </p> <p>Its good approach to use type2enum and enum2type mapping:</p> <p>type2enum:</p> <pre><code>// preparition template&lt;class T&gt; type2enum(); #define TYPE2ENUM_SPEC(TYPE, ENUM) \ template&lt;&gt; type2enum&lt;TYPE&gt;() \ { return ENUM; } enum { T1enum, T2enum, T3enum } TYPE2ENUM_SPEC(type1_t, T1enum); TYPE2ENUM_SPEC(type2_t, T2enum); TYPE2ENUM_SPEC(some_third_type_t, T3enum); </code></pre> <p>and backward enum2type:</p> <pre><code>// preparition template&lt;int Enum&gt; struct enum2type; #define ENUM2TYPE_SPEC(ENUM, TYPE) \ template&lt;&gt; struct Enum2Type&lt;ENUM&gt; \ { typedef TYPE type_t; } // and generic macro #define CREATE_TYPE_MAPPING(TYPE, INTEGER) \ #define TYPE2ENUM_SPEC(TYPE, INTEGER) \ #define ENUM2TYPE_SPEC(INTEGER, TYPE) </code></pre> <p>and the sample of code:</p> <p>we have Type1, Type2, Type3 and enum:</p> <pre><code>enum {T1, T2, T3}; CREATE_TYPE_MAPPING(Type1, T1); CREATE_TYPE_MAPPING(Type2, T2); CREATE_TYPE_MAPPING(Type3, T3); </code></pre> <p>than use it for mapping type to enum:</p> <pre><code>int typeId = type2enum&lt;Type1&gt;(); </code></pre> <p>or constant enum to type:</p> <pre><code>typedef Enum2Type&lt;ConstEnumType&gt;::type_t some_t; </code></pre> <p>and for runtime enum to type you use something like:</p> <pre><code>template&lt;class ObjT&gt; void DoWithTypeIdValue(int enumValue, const ObjT&amp; obj) { switch(enumValue) { case enumType1: obj.do&lt;Enum2Type&lt;enumType1&gt;::type_t&gt;(); break; case enumType2: obj.do&lt;Enum2Type&lt;enumType2&gt;::type_t&gt;(); break; case enumType3: obj.do&lt;Enum2Type&lt;enumType3&gt;::type_t&gt;(); break; default: assert(!"Unknown type constant"); } } </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. 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