title: Conveniently defining bitwise operators for enums for creating bit flags
body: <p>In order to define an enum to be used as bit flags, one would define the enum itself, with appropriate values, and define bitwise operators for the enum type, as below.</p>
  
  <pre><code>enum ENUM_TYPE
  {
      ET_VAL1    = 1L &lt;&lt; 0,
      ET_VAL2    = 1L &lt;&lt; 1
  }
  
  inline ENUM_TYPE operator&amp; (ENUM_TYPE _a, ENUM_TYPE _b) 
  {return ENUM_TYPE(static_cast&lt;int&gt;(_a) &amp; static_cast&lt;int&gt;(_b));}
  
  /* similarly for |, ^, ~, |=, &amp;=, ^= */
  </code></pre>
  
  <p>The problem is that the definition of the bitwise operators takes a lot of space, makes code difficult to read, and is annoying to write, specially if various enums are defined. </p>
  
  <p>One could create a template, as below, in a header file, and simply include that whenever necessary. This introduces the risk of using the bitwise operators on types for which one did not explicitly define them, however.</p>
  
  <pre><code>template&lt;typename ENUM_T&gt;
  inline ENUM_T operator&amp; (ENUM_T _a, ENUM_T _b) 
  {return ENUM_T(static_cast&lt;int&gt;(_a) &amp; static_cast&lt;int&gt;(_b));}
  </code></pre>
  
  <p>One could also create macros, as below. This seems error prone though?</p>
  
  <pre><code>#define BITWISE_AND(TYPE) inline TYPE operator&amp; (TYPE _a, TYPE _b)\
          {return TYPE(static_cast&lt;int&gt;(_a) &amp; static_cast&lt;int&gt;(_b));}
  
  #define BITWISE_ALL(TYPE) BITWISE_AND(TYPE) /* and others */
  </code></pre>
  
  <p>What is the "correct" way to do the above? Are various flag definitions a code smell? What is normally done in production code?</p>