<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- ===================================================================== -->
<!--  File:       MoreSmallAdditionsToISStream.html                        -->
<!--  Author:     J. Kanze                                                 -->
<!--  Date:       03/02/2008                                               -->
<!--      Copyright (c) 2008 James Kanze                                   -->
<!-- _____________________________________________________________________ -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
  <meta http-equiv="content-language" content="en"/>
  <meta name="author" content="J. Kanze"/>
  <meta name="date" content="2008-02-03T16:55:31CET"/>
  <meta name="generator" content="vim"/>
  <title>Some More Small Additions to iostream</title>
  <style type="text/css">
    <!--
    .added { color:darkcyan; text-decoration:underline; }
    .removed { color:red; text-decoration:line-through; }
    .constraint { padding-left:2em; }
    .clause { padding-left:5em; }
    .tblelem { font-family:monospace; vertical-align:top; padding-right:1em; }
    .hlabel { padding-right:1em; text-align:right; }
    -->
  </style>
</head>
<body>
<h1>Some More Small Additions to iostream</h1>
<p>
<table rules="none" style="margin-left:60%">
  <tr><td class="hlabel">Doc. no.:</td><td>N2580=08-0090</td></tr>
  <tr><td class="hlabel">Date:</td><td>2007-03-06</td></tr>
  <tr><td class="hlabel">Author:</td><td>James Kanze</td></tr>
  <tr><td class="hlabel">email:</td><td><a href="mailto:james.kanze@gmail.com">james.kanze@gmail.com</a></td></tr>
</table>
</p>
<hr>
<h2>Remove undefined behavior when inputing an integer</h2>
<h3>Motivation and scope</h3>
<p>
  At present, it is impossible to input an integer without risk of
  undefined behavior, because the semantics in the C++ standard are
  defined in terms of <tt>scanf</tt>, and in the C standard,
  <tt>scanf</tt> says "If this object does not have an appropriate type,
  or if the result of the conversion cannot be represented in the
  object, the behavior is undefined."  This means that unless the
  program can determine in advance that the value will fit, it cannot be
  sure that undefined behavior will not occur.  Given that the standard
  (including the C standard) already requires <tt>stdtol</tt> and
  <tt>stdtoul</tt> to detect overflow, therer doesn't seem to be any
  reason for not requiring it to be detected here.  (Note that the text
  in the C standard also gives "does not have an appropriate type".  I
  suspect that even in C, this is the prime motivation for the undefined
  behavior, and of course, in C++, this motivation is irrelevant, since
  our conversions are typesafe.)
</p>
<h3>Proposed text</h3>
<p>
  In [facet.num.get.virtuals], paragraph 3, first point in Stage 3:
</p>
<blockquote>
  <ul>
    <li>
      A sequence of chars has been accumulated in stage 2 that is
      converted (according to the rules of scanf<span class="added">,
      except that if the results of the conversion cannot be represented
      in the target type, the code will behave as if scanf reported an
      error, as described in the following bullet</span>) to a value of
      the type of val. This value is stored in val and ios_base::goodbit
      is stored in err.
    </li>
  </ul>
</blockquote>
<h2>Add a manipulator to set <tt>basefield</tt> to 0</h2>
<h3>Motivation and scope</h3>
<p>
  Currently, if the <tt>basefield</tt> in the format flags of an
  <tt>istream</tt> (actually in the base class of <tt>istream</tt>) is 0
  (or <tt>fmtflags()</tt>), input of an integral types is "as if" a
  format specifier "%i" was used with <tt>scanf</tt>, in other words, as
  if 0 were passed as the base to <tt>stdtol</tt> or <tt>stdtoul</tt>,
  and as the C++ compiler reads its integral literals, with recognition
  of a leading "0" for octal, or a leading "0x" or "0X" for hexadecimal.
  Regretfully, there is no easy way for the user to set this: the value
  in this case doesn't have a name (e.g. like
  <tt>std::ios_base::dec</tt>, <tt>std::ios_base::hex</tt> or
  <tt>std::ios_base::oct</tt>), nor is there a manipulator to set it.
  The goal of this proposal is simply to give it a name
  (<tt>basefromuser</tt>, since I can't think of anything better), and
  provide a manipulator and a bit mask with the name, to make use easier
  and more intuitive.
</p>
<h3>Proposed text</h3>
<p>
  In [ios.base], in the class definition, add:
</p>
<blockquote>
  <pre>
    namespace std {
      class ios_base {
      public:
        class failure;

        typedef T1 fmtflags;
        <span class="added">static const fmtflags basefromuser;</span>
        static const fmtflags boolalpha;
        static const fmtflags dec;
        //  ...
      };
    }
  </pre>
</blockquote>
<p>
  In [ios::fmtflags], in table 111, fmtflag effects:
</p>
<blockquote>
  <table>
    <tr>
      <th>Element</th>
      <th>Effect(s) if set</th>
    </tr>
    <tr class="added">
      <td class="tblelem"><tt>basefromuser</tt></td>
      <td>
        converts integer input according to the rules which apply to
        literal constants in C++ [lex.icon]. (Note: for reasons of
        backward compatibility, this flag is required to have a numeric
        value of 0.)
      </td>
    <tr>
      <td class="tblelem"><tt>boolalpha</tt></td>
      <td>insert and extract <tt>bool</tt> bool type in alphabetic
        format</td>
    </tr>
    <tr><td align="center" colspan="2"><strong>...</strong></td></tr>
  </table>
</blockquote>
<p>
  In [basefield.manip], add:
</p>
<blockquote class="added">
  <p>
    <tt>ios_base&amp; basefromuser(ios_base&amp; str);</tt>
  </p>
  <p class="constraint">
    <i>Effects:</i> Calls <tt>str.setf( ios_base::basefromuser,
      iso_base::basefield )</tt>.
  </p>
  <p class="constraint">
    <i>Returns:</i> <tt>str</tt>.
  </p>
</blockquote>
<h2>Add a manipulator to set <tt>floatfield</tt> to 0</h2>
<h3>Motivation and scope</h3>
<p>
  Currently, if the <tt>floatfield</tt> in the format flags of an
  <tt>ostream</tt> (actually in the base class of <tt>ostream</tt>) is
  0 (or <tt>fmtflags()</tt>), output of floating point values is "as if"
  a format specifier "%g" was used with <tt>printf</tt>; in other words,
  the style will be either fixed or scientific depending on the value
  converted.  Regretfully, although this is the initial state, there is
  no easy way for the user to force this value if the initial state has
  been changed; the value in this case doesn't have a name (e.g. like
  <tt>std::ios_base::fixed</tt> or <tt>std::ios_base::scientific</tt>),
  nor is there a manipulator to set it.  The goal of this proposal is
  simply to give it a name (<tt>adaptive</tt>, since I can't think of
  anything better), and provide a manipulator and a bit mask with the
  name, to make it easier to use and more intuitive.
</p>
<h3>Proposed text</h3>
<p>
  In [ios.base], in the class definition, add:
</p>
<blockquote>
  <pre>
    namespace std {
      class ios_base {
      public:
        class failure;

        typedef T1 fmtflags;
        <span class="added">static const fmtflags adaptive;</span>
        static const fmtflags boolalpha;
        static const fmtflags dec;
        //  ...
      };
    }
  </pre>
</blockquote>
<p>
  In [ios::fmtflags], in table 111, fmtflag effects:
</p>
<blockquote>
  <table>
    <tr>
      <th>Element</th>
      <th>Effect(s) if set</th>
    </tr>
    <tr class="added">
      <td class="tblelem"><tt>adaptive</tt></td>
      <td>
        Forces output conversion in the same way as is done by the
        <tt>"%g"</tt> or <tt>"%G"</tt> format of <tt>fprintf</tt> in C.
        (Note: for reasons of backward compatibility, this flag is
        required to have a numeric value of 0.)
      </td>
    <tr>
      <td class="tblelem"><tt>boolalpha</tt></td>
      <td>insert and extract <tt>bool</tt> bool type in alphabetic
        format</td>
    </tr>
    <tr><td align="center" colspan="2"><strong>...</strong></td></tr>
  </table>
</blockquote>
<p>
  In [basefield.manip], add:
</p>
<blockquote class="added">
  <p>
    <tt>ios_base&amp; adaptive(ios_base&amp; str);</tt>
  </p>
  <p class="constraint">
    <i>Effects:</i> Calls <tt>str.setf( ios_base::adaptive,
      iso_base::floatfield )</tt>.
  </p>
  <p class="constraint">
    <i>Returns:</i> <tt>str</tt>.
  </p>
</blockquote>
</body>
</html>
<!-- Local Variables:               === for emacs -->
<!-- mode: html                     === for emacs -->
<!-- tab-width: 8                   === for emacs -->
<!-- End:                           === for emacs -->
<!-- vim: set ts=8 filetype=html:   === for vim   -->
