Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With macro, you can write a beautiful solution for problem such as this:</p> <ul> <li>Define an enum such that its value can be converted into its string representation and vice-versa.</li> </ul> <p>Suppose, you want to define an enum called <code>period</code> whose members are <code>one</code>, <code>five</code>, <code>ten</code>, <code>fifteen</code> and <code>thirty</code>. Then here is how you do it:</p> <ul> <li><p>First create a header file called <code>period_items.h</code> as:</p> <pre><code>//period_items.h //Here goes the items of the enum //it is not a definition in itself! E(one) E(five) E(ten) E(fifteen) E(thirty) </code></pre></li> <li><p>then create another header file called <code>period.h</code> as:</p> <pre><code>//period.h #include &lt;string&gt; //HERE goes the enum definition! enum period { #define E(item) item, #include "period_items.h" //it dumps the enum items in here! #undef E period_end }; period to_period(std::string const &amp; name) { #define E(item) if(name == #item) return item; #include "period_items.h" #undef E return period_end; } </code></pre></li> </ul> <p>Now you can simply include <code>period.h</code> and use <code>to_period</code> function. :-)</p> <p>You could also add this function to <code>period.h</code> as:</p> <pre><code>std::string to_string(period value) { #define E(item) if(value == item) return #item; #include "period_items.h" #undef E return "&lt;error&gt;"; } </code></pre> <p>Now, you could write this:</p> <pre><code>#include "period.h" period v = to_period("fifteen"); //string to period std::string s = to_string(v); //period to string </code></pre> <p><strong>Why this solution is beautiful?</strong></p> <p>Because now if you want to add few more members to the enum, all you have to do is to add them to <code>period_items.h</code> as:</p> <pre><code> //period_items.h //Here goes the items of the enum //it is not a definition in itself! E(one) E(five) E(ten) E(fifteen) E(thirty) E(fifty) //added item! E(hundred) //added item! E(thousand) //added item! </code></pre> <p>And you're done. <code>to_string</code> and <code>to_period</code> will work just fine, without any modification!</p> <p>--</p> <p>I took this solution from my solution to another problem, posted here:</p> <ul> <li><a href="https://stackoverflow.com/a/10887421/415784">converting from enum to int</a></li> </ul>
    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. 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