title: C++ implicit conversion to bool
body: <p>In an effort to make my enums more typesafe, I've been using macro-generated overloaded operators to disallow comparing enums against anything but an identically typed enum:</p>
  
  <pre><code>#include &lt;boost/static_assert.hpp&gt;
  
  #define MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, op) \
    template&lt;typename T&gt; \
    inline bool operator op(enumtype lhs, T rhs) \
    { \
      BOOST_STATIC_ASSERT(sizeof(T) == 0); \
      return false; \
    } \
    \
    template&lt;&gt; \
    inline bool operator op(enumtype lhs, enumtype rhs) \
    { \
      return static_cast&lt;int&gt;(lhs) op static_cast&lt;int&gt;(rhs); \
    }
  
  #define MAKE_ENUM_TYPESAFE(enumtype) \
    MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, ==) \
    MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, !=) \
    MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, &gt;) \
    MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, &lt;) \
    MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, &gt;=) \
    MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, &lt;=)
  
  // Sample usage:
  enum ColorType { NO_COLOR, RED, BLUE, GREEN };
  MAKE_ENUM_TYPESAFE(ColorType)
  </code></pre>
  
  <p>This generally has the desired effect; comparisons of the form <code>color_variable == RED</code> work, while comparisons of the form <code>color_variable == 1</code> generate compile-time errors thanks to <a href="http://www.boost.org/doc/libs/1_40_0/doc/html/boost_staticassert.html" rel="noreferrer">Boost.StaticAssert</a>.  (Is this a decent approach?)</p>
  
  <p>However, my compiler (CodeGear C++Builder) is also trying to use these overloaded operators to implement implicit <code>bool</code> conversions.  For example, <code>if (color_variable) { ... }</code> is being translated to <code>if (operator!=(color_variable, 0)) { ... }</code> and is triggering the <code>BOOST_STATIC_ASSERT</code> and failing to compile.</p>
  
  <p>I'm fairly certain that this is incorrect behavior on the part of my compiler (Comeau and GCC don't do it, for example) but was wondering if there are any language lawyers present who could confirm.  I tried looking up in the C++0x draft standard myself, but all I could find was the following statement under section 4.12:</p>
  
  <blockquote>A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.</blockquote>
  
  <p>without details as to how "a zero value" is checked for.</p>