<p>What's the best way to use NaNs in C++?</p>

<p>I found <code>std::numeric_limits&lt;double&gt;::quiet_NaN()</code> and <code>std::numeric_limits&lt;double&gt;::signaling_NaN()</code>.  I'd like to use <code>signaling_NaN</code> to represent an uninitialized variable as follows:</p>

<pre><code>double diameter = std::numeric_limits&lt;double&gt;::signaling_NaN();
</code></pre>

<p>This, however, signals (raises an exception) on assignment.  I want it to raise an exception on use, not on assignment.</p>

<p>Is there any way to use <code>signaling_NaN</code> without raising an exception on assignment?  Is there a good, portable alternative to <code>signaling_NaN</code> that will raise a floating point exception when used?</p>

You can write a signalling NaN into a variable without triggering an exception with something like this (nb: untested)
```
```
void set_snan( double &d )
{
    long long *bits = (long long *)&d;
    *bits = 0x7ff0000080000001LL;
}
```
```
It'll work most places, but no, it's not 100% portable.