    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: The Casting Vote</title>
        <link>https://members.accu.org/index.php/articles/1438</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #4 - Feb 1994</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c233/">04</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-233/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+233/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;The Casting Vote</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 01 February 1994 08:59:00 +00:00 or Tue, 01 February 1994 08:59:00 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<p>San Jose - November 1993 - Sean A. Corfield</p>
<p>The announcement of Borland C++ 4.0 managed to permeate the November
meeting of the joint ISO and ANSI committees. Borland's representative
disappeared for one day - everyone joked that this was in order to
ensure that the forthcoming release matched the decisions of the
committee as closely as possible. There is no doubt that Borland have
become the leading edge commercial compiler - they've even managed to
include some features that were only voted into the draft standard at
the San Jose meeting - new style cast syntax, more of which later.</p>
<p>What were the decisions made at San Jose and how will they affect
you?</p>
<h2>1. Library</h2>
<p>The standard library is still going through some turmoil. Minor
changes were made to the standard string class and the dynamic array
class (dynarray).</p>
<p>The string class now has two index operators:</p>
<pre>char  operator[](size_t) const; <br>char&amp; operator[](size_t);</pre>
<p>&quot;About time&quot;, many people will say. Complex number classes have been
added which match the proposed NCEG (Numerical C Extensions Group)
builtin complex data types. A stringstream class has been added to
allow streams to be used with the builtin string class for all the code
where we currently like to use &quot;sprintf&quot;. Streams themselves are still
being worked on, however, in the face of internationalisation (often
referred to as &quot;il8n&quot; because 18 letters are omitted from the
abbreviation!).</p>
<p>&quot;il8n&quot; might not seem like a very interesting topic - wide
characters and multi-byte characters - but it generates a lot of
discussion. At the moment, two somewhat different approaches to streams
have been proposed. The committee hopes to settle this fairly soon, but
there do appear to be some personal / political issues which could
become a sticking point - I will say no more!</p>
<p>Other changes in the standard library will have a wider impact:</p>
<p>namespaces and exceptions. A proposal has been accepted to &quot;wrap&quot;
each standard header in a namespace to reduce pollution of the global
scope. To provide a transition, old-style headers will be provided that
include the new-style header and then open up the namespace, something
along the lines of:</p>
<pre>// math -- new header <br>namespace iso::c::math {<br>  double acos(double);<br>  // etc<br>}</pre>
<pre><br>// math.h<br>// -- include namespace version of<br>//&nbsp;&nbsp;  math header<br>#include &lt;math&gt;<br>// -- open up the namespace:<br>using namespace iso::c::math;<br><br>// prog.c<br>#include &lt;math.h&gt; <br>// doesn't need to change <br>... acos(0.5) ...</pre>
<p>Why is this useful? Each header currently declares all its names in
the global namespace which would prevent you from using, say, a faster
maths library implementation, e.g.,</p>
<pre>// fastmath <br>namespace fastmath {<br>  double acos(double);<br>  // etc <br>}<br><br>// prog.c<br>#include &lt;math.h&gt;<br>#include &lt;fastmath&gt;<br>// -- override some declarations:<br>using fastmath::acos;<br>using fastmath::asin;<br>// etc<br>... acos(0.5) ...// uses fastmath::acos</pre>
<p>Because of the way namespaces are defined to work, this allows you
to mix'n'match between libraries in a well-defined manner. The general
approach has been accepted but there may be name changes before the
standard is produced. By the way, namespace was voted into the draft
standard in July 1993 - the last &quot;big&quot; extension.</p>
<p>The exceptions proposal for the library will provide a single class
hierarchy of exception, currently with xmsg at the root, which
partitions exceptions that can be thrown by the library routines into
subcategories such as domain and range. Again, the principle has been
accepted but the exact details are still to be finalised. In the light
of a PC compiler that supports exception handling, this is an important
issue - you need to know the exception inheritance hierarchy in order
to be able to write code that robustly handles library exceptions.</p>
<h2>2. Templates</h2>
<p>The Extensions Working Group completed the majority of the issues
that were outstanding on templates.&nbsp;&nbsp; This will result in
Chapter 14 Templates being completely rewritten between now and March
1994 in order to incorporate the resolutions. The new chapter will
include some extensions that are required to make templates useful for
real work.</p>
<p>In case you think I'm being flippant, I should probably explain that
remark: almost every implementation already provides some extensions
but they are incompatible with each other - the committee is trying to
standardise these extensions to make useful programs portable.</p>
<p>One major area which has been clarified is &quot;name binding&quot;. This
determines whether an identifier in a template definition is looked up
at the definition point or at the instantiation point. Examples to
explain this are complicated, but the bottom line is that with a few
minor details still to be resolved, the compiler will be able to syntax
check a template definition for you, even if you do not instantiate it.</p>
<p style="font-style: italic;">What extensions?</p>
<p>You can now explicitly ask for a template to be instantiated rather
than relying on the compiler to figure it out:</p>
<pre>template&lt;class T&gt; class Thing {...}; <br>// request instantiation <br>template class Thing&lt;int&gt;;</pre>
<p>This should make the compile-and-link cycle faster because the
compiler can perform all the instantiations &quot;on-the-fly&quot; rather than
having the pre-link phase run the compiler for each template that is
required to be instantiated.<br>
</p>
<p>You can now declare an intent to specialise a template:</p>
<pre>// template Thing as above:<br>// I will provide my own definition<br>class Thing&lt;char*&gt;;</pre>
<p>This helps the compiler decide what assumptions it can make when it
later sees:</p>
<pre>Thing&lt;char*&gt;&nbsp; x;</pre>
<p>In fact, the resolution will break a lot of code - you are now
REQUIRED to declare your intent to specialise a template. This used to
work:</p>
<pre>// thing.h<br>template&lt;class T&gt; class Thing {...};<br><br>// thing.c <br>#include &lt;thing.h&gt; <br>class Thing&lt;char*&gt; {<br>  // specialised definition <br>};<br><br>// main.c<br>#include &lt;thing.h&gt; <br>Thing&lt;char*&gt;&nbsp; a;</pre>
<p>The specialisation would be linked in, although the class interface
used would have come from &lt;thing.h&gt; so strange things could
happen! Under the new resolution, the compiler can assume that what it
sees in &lt;thing.h&gt; will be what is used to instantiate the
template and will NOT link in the specialisation!</p>
<p>Both of the above are supported in different forms by many
compilers, but the following extension is entirely new: explicit
qualification of function templates. Currently, the following is
illegal:</p>
<pre>template&lt;class T&gt; T* factory();</pre>
<p>This is because template parameters are required to appear in the
function parameter list so that the types can be deduced when a
function call is seen. The extension allows the following to be written:</p>
<pre>int* pi = factory&lt;int&gt;(); <br>Shape* ps = factory&lt;Shape&gt;(); <br>Car* pc = factory&lt;Car&gt;();</pre>
<p>The &quot;factory&quot; will produce whatever type you want (subject, of
course, to successful instantiation of the function template). The
syntax of explicit qualification has some bearing on the extensions
adopted for instantiation requests and specialisation described above,
and for the next item I shall talk about: new style casts.</p>
<h2>3. New Style Casts</h2>
<p>This was the most controversial item voted on. The presentation of
the proposal had generated a lot of discussion during the week but when
it was presented for formal vote split the ANSI committee almost
exactly down the middle (the ISO committee were in favour: six for, one
against and one abstain). Some further discussion ensued when it was
discovered that one ANSI member did not have voting rights and that the
ANSI vote would have to be counted again! Fortunately, Bjarne
Stroustrup was able to sway several committee members and the final
vote went 3 : 2 in favour at ANSI (the ISO vote was unchanged).</p>
<p style="font-style: italic;">What are the new style casts?</p>
<p>In March 1993, run-time type identification was voted into the draft
which introduced the unusual looking notation:</p>
<pre>if (X* = dynamic_cast&lt;X*&gt;(p)) { ... }</pre>
<p>The new style casts also look like that, with three new operators as
follows:</p>
<pre>static_cast&lt;type&gt; (expression)<br>const_cast&lt;type&gt;(expression) <br>reinterpret_cast&lt;type&gt;(expression)</pre>
<p>The idea behind these is to enhance the safety of casting.
Currently, if you see:</p>
<pre>Y* py; <br>// . . . <br>X* px = (X*)py;</pre>
<p>You cannot tell what the cast will do. Depending on whether X and Y
are complete types (i.e., their definition is visible) and whether they
are related, the cast will change its behaviour. Indeed, simply adding
an extra include file could cause the cast to change behaviour!</p>
<p>The new casts come with a set of restrictions on what they can
actually do which are, in simple terms, as follows:</p>
<p>const_cast is the simplest to explain - it allows you to adjust the
const and volatile qualifiers and nothing else, e.g.,</p>
<pre>class A {...};<br>class B : public A {...};<br>A*&nbsp;&nbsp; &nbsp;pa;<br>B*&nbsp;&nbsp; &nbsp;pb;<br>int const* volatile* pvpci;<br>// ...<br>const_cast&lt;A const*)(pa);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //OK<br>const_cast&lt;int**&gt;(pvpci)&nbsp;&nbsp; &nbsp;      // OK<br>const_cast&lt;int const**&gt;(pvpci)&nbsp;&nbsp;&nbsp; // OK<br>const_cast&lt;A const*&gt;(pb);&nbsp;&nbsp; &nbsp;     // error!<br>const_cast&lt;char const*&gt;<br>                (volatile*)(pvpci)//error!</pre>
<p>The last two are errors because they change more than just the
qualifiers.</p>
<p>static_cast is related to implicit casts - if you can do an implicit
cast between two types (in one direction), static_cast allows you to go
in both directions so long as you do not cast away const, e.g.,</p>
<pre>B const b2(&quot;constant thing&quot;);<br>// implicit B const* -&gt; A const*<br>A const* pa2 = &amp;b2;<br>// ...<br>static_cast&lt;A*&gt;(pb)&nbsp;&nbsp; &nbsp;//OK<br>static_cast&lt;B*&gt;(pa)&nbsp;&nbsp; &nbsp;// downcast - also OK<br>static_cast&lt;B*&gt;(pa2)&nbsp;&nbsp; // error!</pre>
<p>The latter is an error because it casts away constness. You can say:</p>
<pre>static_cast&lt;B*&gt;(const_cast&lt;A*&gt;(pa2))</pre>
<p>or</p>
<pre>const_cast&lt;B*&gt;(static_cast&lt;B const*&gt;(pa2))</pre>
<p>This is much more explicit - see how easy it was to write an unsafe
program before:</p>
<pre>(A*)pb  // OK - redundant upcast<br>(B*)pa  // OK - we know it's a B really<br>(B*)pa2 // OK? oops! now we can modify b2!</pre>
<p>The final cast is reinterpret_cast - it can do all the casts that
you cannot do with static_cast, but it also cannot cast away constness.</p>
<p>The committee were unable to agree that deprecating the C-style
would be a good move and that part of the proposal was dropped, but
even so, I would recommend using the new style casts as soon as you get
Borland 4.0 because they will catch a lot of bugs that would otherwise
be very hard to track down.</p>
<p>Note that Bjarne Stroustrup's &quot;hard&quot; problem (from the Interview in
Overload #2) with adding const in a pointer to function cast was
decided to be a non-problem. In other words, reinterpret_cast is
allowed to do that type of cast (and const_cast is not) because when
you change the constness of function parameters you simply get a
different type of function and the standard already says that it is
undefined if you attempt to call a function through such a pointer to
function - you have to cast it back to the original type first. Whilst
this leaves a very small hole in the const-safety of the new casts, it
is considered to be obscure enough not to matter, and the benefits
outweigh the problems.</p>
<h2>4. Boolean</h2>
<p>A proposal to introduce a builtin boolean type was considered and
accepted (3 : 2 at ANSI; 7 for, 1 against, no abstain at ISO). This
provides a builtin integral type called &quot;bool&quot; and two reserved words
&quot;true&quot; and &quot;false&quot;. Those of you with a background in languages like
Pascal will cheer, others will be stunned / shocked / delete where
applicable. The UK panel discussed the possibility of a builtin boolean
type over a year ago but could not see how to integrate it into the
language. Andrew Koenig of AT&amp;T and Dag Bruck of Dynasim AB,
Sweden, solved the problem beautifully by introducing implicit
conversions from integral and pointer types to bool in certain
circumstances.</p>
<p>The following operators now return a bool result:</p>
<pre>&lt;&nbsp;&nbsp; &nbsp;&lt;=&nbsp;&nbsp; &nbsp;&gt;&nbsp;&nbsp; &nbsp;&gt;=&nbsp;&nbsp; &nbsp;==&nbsp;&nbsp; &nbsp;! =<br>&amp;&amp;&nbsp;&nbsp; &nbsp;||&nbsp;&nbsp; &nbsp;!</pre>
<p>The latter three also require bool operands (but will implicitly
convert integral and pointer types if necessary), as does ? :</p>
<p>All the conditional constructs (if, while, for, do while) require a
bool expression (with conversion if necessary). bool converts
implicitly to int, with true becoming 1 and false becoming 0 - this
preserves existing code that uses int (or char) instead of some boolean
typedef. There are a few quirks that will arise from the adoption of
bool - floating point types do not convert:</p>
<pre>double d;<br>if (d) // sloppy! tests d != 0.0<br>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // becomes an error with bool type<br>}<br>int i ;<br>i = d; // converts d to int - not good if i<br>       //is used as boolean <br>if (i) // huh?<br>{<br>}<br>bool b;<br>b = d; // error - cannot convert!</pre>
<p>This is good news - you cannot reliably test floating point numbers
for equality and converted floating point numbers do not make good
true/false values!</p>
<p>Another quirk is that ++ will be defined to mean &quot;become true&quot; to
allow the following current (sloppy) practice:</p>
<pre>bool matches = false;<br>for (list* p = head; p; p = p-&gt;next) {<br>  if (p-&gt;item == key) {<br>    ++matches;<br>  } <br>}<br>if (matches) { // found any matches for key? <br>}<br></pre>
<p>Note that ++ is deprecated. You will eventually have to rewrite this:</p>
<pre>int count =0; // count the entries, or... <br>bool matches = false; // ..use a flag <br>for (list* p = head; p; p = p-&gt;next) { <br>  if (p-&gt;item == key) {<br>    // increment the count, or...<br>    ++count;<br>    matches = true; // set the flag <br>  } <br>}<br>// valid use of bool matches, or... <br>if (matches) {<br>}<br>// ...alternate use of a count <br>if (count &gt; 0) { <br>}</pre>
<p>The choice is probably stylistic. Again, the justification for
introducing bool is type safety - if you really want a boolean valued
variable, now you can declare one and have the compiler check that you
are using it consistently.</p>
<h2>5.&nbsp;&nbsp; &nbsp;Implicit int</h2>
<p>The joint committee decided (by almost unanimous vote) to deprecate
implicit int everywhere and ban it altogether in two places. In fact,
the actual deprecation covers omitted type-specifiers so it will be
acceptable to say:</p>
<pre>short s = 0;</pre>
<p>but you will get a compiler warning (probably) if you say:</p>
<pre>const c = 42;</pre>
<p>or</p>
<pre>static x = 1;</pre>
<p>The two places where omitting the type-specifier has been banned
altogether are:</p>
<pre>// file scope:<br>f();&nbsp;&nbsp; // previously meant int f();<br>a;&nbsp;&nbsp;&nbsp;&nbsp; // previously meant int a;</pre>
<p>and</p>
<pre>// previously meant typedef int Int;<br>typedef Int;</pre>
<p>The first two are not allowed by ISO C and not believed to be common
in C++, the latter certainly shouldn't be common. Part of the reason
for banning the typedef form was that typedef will be used to resolve
some name binding issues in templates.</p>
<h2>6.&nbsp;&nbsp; &nbsp;Miscellaneous</h2>
<p>The Core Working Group resolved several (generally minor) issues
covering a wide variety of topics: qualifiers, references, lvalue-ness
and so on. Most of the resolutions are intended to codify existing
practice, but it is difficult to know the impact on the language until
the changes are integrated. Personally, I suspect they are not
consistent and we will see some backtracking.</p>
<h2>7. The Working Paper, Editing and The Schedule</h2>
<p>The committee is still intending to vote the Working Paper up to a
Working Draft in July 1994. This is the first step in getting the draft
standard approved as an International Standard. Almost everyone on the
committee believes this is a very aggressive schedule. The ISO
committee members are deeply unhappy with the current state of the
draft and look likely to vote against approving a Working Draft in July
unless some substantial improvements are made before that time. In
order to achieve this, Andrew Koenig, the project editor, has convened
an editorial working group who will edit parts of the draft in parallel
to speed up the process. Volunteers for the work include ISO members
from the UK (myself), the USA and Australia, with both France and
Germany keen to review the changes prior to incorporation into the
draft. This process may well become the critical task in providing a
timely international C++ standard. Wish us luck!</p>
<p>The next international meeting is in March 1994.</p>
<p>Now I will make a general plea: support your national standards body!</p>
<p>If you are interested in attending the UK C++ panel meetings (one
day each, four times a year in London) please contact me for more
information.</p>
<p>I can be contacted by e-mail: Sean.Corfield@prlO.co.uk (yes, I know,
P-R-L-zero is an odd e-mail address!)</p>
<p>(Ed - write to me if you are interested and don't have access to
e-mail)</p></p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
