Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With C++03 you can fairly easily work around the issue with the aid of a helper class and your own operator:</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;limits&gt; struct FloatNaNHelper { float value; operator const float&amp;() const { return value; } }; std::istream&amp; operator&gt;&gt;(std::istream&amp; in, FloatNaNHelper&amp; f) { if (in &gt;&gt; f.value) return in; in.clear(); std::string str; if (!(in &gt;&gt; str)) return in; // use std::transform for lowercaseness? // NaN on my platform is written like this. if (str == "NaN") f.value = std::numeric_limits&lt;float&gt;::quiet_NaN(); else in.setstate(std::ios::badbit); // Whoops, we've still "stolen" the string return in; } </code></pre> <p>This works for NaN on my platform quite sensibly, but also illustrates the portability problem inherent there too - your library seems to represent it differently, which could complicate the issue somewhat if you wanted to support both. I used this test with it:</p> <pre><code>int main() { std::istringstream in("1.0 555 NaN foo"); FloatNaNHelper f1,f2,f3; in &gt;&gt; f1 &gt;&gt; f2 &gt;&gt; f3; std::cout &lt;&lt; static_cast&lt;float&gt;(f1) &lt;&lt; ", " &lt;&lt; static_cast&lt;float&gt;(f2) &lt;&lt; ", " &lt;&lt; static_cast&lt;float&gt;(f3) &lt;&lt; std::endl; if (in &gt;&gt; f1) std::cout &lt;&lt; "OOPS!" &lt;&lt; std::endl; } </code></pre> <p>You can also change the semantics of this to something possibly a little cleaner:</p> <pre><code>int main() { std::istringstream in("1.0 555 NaN foo"); float f1,f2,f3; in &gt;&gt; FloatNaNHelper(f1) &gt;&gt; FloatNaNHelper(f2) &gt;&gt; FloatNaNHelper(f3); std::cout &lt;&lt; f1 &lt;&lt; ", " &lt;&lt; f2 &lt;&lt; ", " &lt;&lt; f3 &lt;&lt; std::endl; } </code></pre> <p>Requires the changing <code>FloatNaNNHelper</code>:</p> <pre><code>struct FloatNaNHelper { float&amp; value; explicit FloatNaNHelper(float&amp; f) : value(f) { } }; </code></pre> <p>And the operator:</p> <pre><code>std::istream&amp; operator&gt;&gt;(std::istream&amp; in, const FloatNaNHelper&amp; f); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload