title: Is there a difference between `static_cast<bool>(x)` and `x != 0.0`?
body: <h2>The Setup</h2>
  
  <p>I'm working with the API for an application called MotionBuilder. In order to access a MotionBuilder property's value, you read it into a double variable, regardless of what sort of data type it actually represents.</p>
  
  <p>Here's a utility function I wrote to evaluate the value of a scalar property:</p>
  
  <pre><code>template &lt;typename DataT&gt;
  inline DataT GetScalar(FBProperty&amp; prop, FBEvaluateInfo* evaluateInfo)
  {
      double data = 0.0;
      prop.GetData(&amp;data, sizeof(data), evaluateInfo);
      return static_cast&lt;DataT&gt;(data);
  }
  </code></pre>
  
  <p>This way, I can write <code>GetScalar&lt;float&gt;(camera.Roll, evaluateInfo)</code> or <code>GetScalar&lt;bool&gt;(camera.Visibility, evaluateInfo)</code> instead of having a multiple-line mess of uninitialized buffers and casts littering my code.</p>
  
  <p>I'm compiling in Visual Studio with <code>/W4</code> and addressing all warnings as they come up. When I use <code>GetScalar&lt;bool&gt;</code>, the compiler produces <a href="http://msdn.microsoft.com/en-us/library/b6801kcy%28v=vs.100%29.aspx" rel="nofollow">a C4800 warning</a>:</p>
  
  <pre><code>'double' : forcing value to bool 'true' or 'false' (performance warning)
  </code></pre>
  
  <p>When the compiler creates <code>GetScalar&lt;bool&gt;</code>, it winds up with a static_cast from double to bool, which it apparently doesn't like. Since my original aim was to handle multiple types (bool, float, double, etc) with a single template function, I can't just add in the usual <code>!= 0.0</code>.</p>
  
  <p>In order to address this warning, I have two options.</p>
  
  <h2>Option 1</h2>
  
  <p>I can suppress the warning directly with pragmas, since the cast is doing exactly what I wanted it to do:</p>
  
  <pre><code>template &lt;typename DataT&gt;
  inline DataT GetScalar(FBProperty&amp; prop, FBEvaluateInfo* evaluateInfo)
  {
      double data = 0.0;
      prop.GetData(&amp;data, sizeof(data), evaluateInfo);
  #pragma warning (push)
  #pragma warning (disable: 4800) // Don't complain about casting to bool
      return static_cast&lt;DataT&gt;(data);
  #pragma warning (pop)
  }
  </code></pre>
  
  <h2>Option 2</h2>
  
  <p>I can add a specialization of <code>GetScalar</code> to handle the <code>bool</code> case:</p>
  
  <pre><code>template &lt;&gt;
  inline bool GetScalar&lt;bool&gt;(FBProperty&amp; prop, FBEvaluateInfo* evaluateInfo)
  {
      double data = 0.0;
      prop.GetData(&amp;data, sizeof(data), evaluateInfo);
      return data != 0.0;
  }
  </code></pre>
  
  <h2>The Question</h2>
  
  <p>I would think that for some double x, <code>static_cast&lt;bool&gt;(x)</code> is exactly equivalent to <code>x != 0.0</code>. In fact, a simple test compiled in release mode gives me the same assembly output in both cases. Why, then, does C4800 call itself a "performance warning?" Are the two options outlined above functionally identical? If it comes down to a matter of style, after putting on your best pedant hat, which option do you prefer?</p>