title: Template for type safe c++11 enum class flags
body: <p>I am trying to create type safe C++ flags using templates. I also want to distinguish between <em>a</em> flag and flag<em>s</em> (being zero, one or many flags).</p>
  
  <p>The solution below works good, except for <code>EnumFlag&lt;T&gt; operator | (T, T)</code> which causes all <code>|</code>-operations on enums to return type <code>EnumFlag</code>. This breaks lots of code. Any tricks to fix this? In my code I do the following, however hard coding <code>Option</code> here is not an option. How to make this generic?</p>
  
  <pre><code>EnumFlag&lt;typename std::enable_if&lt;std::is_same&lt;T, Option&gt;::value, T&gt;::type&gt; operator | (T l, T r)
  </code></pre>
  
  <p>Changing this to...</p>
  
  <pre><code>EnumFlag&lt;T&gt; operator | (T l, T r)
  </code></pre>
  
  <p>...of cause breaks everything. I would like something like this (not compilabel code). Or any other better idea!</p>
  
  <pre><code>EnumFlag&lt;typename std::enable_if&lt;std::already_expanded&lt;EnumFlag&lt;T&gt;&gt;::value, T&gt;::type&gt; operator | (T l, T r)
  </code></pre>
  
  <p>Complete compilable code:</p>
  
  <p><strong>EnumFlag.h</strong></p>
  
  <pre><code>#ifndef __classwith_flags_h_
  #define __classwith_flags_h_
  
  #include &lt;type_traits&gt;
  
  enum class Option
  {
      PrintHi = 1 &lt;&lt; 0,
      PrintYo = 1 &lt;&lt; 1,
      PrintAlot = 1 &lt;&lt; 2
  };
  
  template &lt;typename T&gt;
  class EnumFlag
  {
  public:
      using UnderlayingType = typename std::underlying_type&lt;T&gt;::type;
  
      EnumFlag(const T&amp; flags)
          : m_flags(static_cast&lt;UnderlayingType&gt;(flags))
      {}
  
      bool operator &amp; (T r) const
      {
          return 0 != (m_flags &amp; static_cast&lt;UnderlayingType&gt;(r));
      }
  
      static const T NoFlag = static_cast&lt;T&gt;(0);
  
  private:
      UnderlayingType  m_flags;
  };
  template&lt;typename T&gt;
  EnumFlag&lt;typename std::enable_if&lt;std::is_same&lt;T, Option&gt;::value, T&gt;::type&gt; operator | (T l, T r)
  {
      return static_cast&lt;T&gt;(static_cast&lt;typename EnumFlag&lt;T&gt;::UnderlayingType&gt;(l) | static_cast&lt;typename EnumFlag&lt;T&gt;::UnderlayingType&gt;(r));
  }
  
  class ClassWithFlags
  {
  public:
      using Options = EnumFlag &lt; Option &gt;;
  
      void doIt(const Options &amp;options);
  };
  
  #endif
  </code></pre>
  
  <p><strong>EnumFlag.cpp</strong></p>
  
  <pre><code>#include "EnumFlag.h"
  
  #include &lt;iostream&gt;
  
  void ClassWithFlags::doIt(const Options &amp;options)
  {
      if (options &amp; Option::PrintHi)
      {
          std::cout &lt;&lt; "Hi" &lt;&lt; std::endl;
      }
      if (options &amp; Option::PrintYo)
      {
          std::cout &lt;&lt; "Yo!" &lt;&lt; std::endl;
      }
  }
  
  int main()
  {
      ClassWithFlags classWithFlags;
      classWithFlags.doIt(Option::PrintHi | Option::PrintAlot);
  }
  </code></pre>
  
  <p><strong>> <a href="http://coliru.stacked-crooked.com/a/985a3d21d402d2f4" rel="nofollow">DEMO</a> &lt;</strong></p>
  
  <p>The actual code will contain a lot more operators, however this is enough to illustrate the problem.</p>
  
  <p>One less intrusive solution is this (but still too intrusive)</p>
  
  <pre><code>template&lt;typename T&gt;
  typename std::underlying_type&lt;T&gt;::type operator | (T l, T r)
  {
      return (static_cast&lt;typename std::underlying_type&lt;T&gt;::type&gt;(l) | static_cast&lt;typename std::underlying_type&lt;T&gt;::type&gt;(r));
  }
  </code></pre>
  
  <p>Not god enough, then <code>EnumFlag(const std::underlying_type&lt;T&gt; &amp;flags)</code> must exist and I lose type safty. Also, I would like for the global operator overloads to only be created for the types actually needed. Macros is also no god because I want ta allow declaration of <code>EnumFlag</code>s inside classes. The global overloads can not be there, hence I need two macros calls at different locations to create on <code>EnumFlag</code>.</p>
  
  <p>The solution must be pure C++11/stl.</p>