    <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  :: Questions &amp; Answers</title>
        <link>https://members.accu.org/index.php/articles/917</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">CVu Journal Vol 11, #5 - Aug 1999</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/c76/">Journals</a>

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c130/">115</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;Questions &amp; Answers</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 06 August 1999 13:15:32 +01:00 or Fri, 06 August 1999 13:15:32 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p><span class="emphasis"><em>It would help with this column if
those of you who either answer questions that others ask you or
have your questions asked could take the time to write up the
question and the answer. If you want your name can be with-held.
This column is about real questions and real
answers.</em></span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e23" id="d0e23"></a>From: Peter
Hudson <tt class="email">&lt;<a href=
"mailto:PeterH@wagm.com">PeterH@wagm.com</a>&gt;</tt></h2>
</div>
<p>Dear Francis,</p>
<p>As a man who knows many things, I wondered if I might ply you
with my latest problem.</p>
<p>I have benefited no end from your suggestion that Leen
Ammeraals' book on STL would be a good place to start STL. Since
ploughing through this tome I have tackled others.</p>
<p>One facet of all these books is however consistent:</p>
<p>None spend any time explaining how to use STL with user defined
types. They all stick to ints, chars, doubles and the like. I can
find only a short piece in Lippman on specialisation (which may or
may not be the technique I need to use). Even Bjarne does not refer
to it.</p>
<p>Are you aware of any books on this subject? Or am I just looking
at this the wrong way? Or am I just crying for the moon? Or do I
have the wrong end of the stick entirely?</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e40" id="d0e40"></a>Answer from
Francis Glassborow</h3>
</div>
<p>I think there are two parts to this question, the first is 'how
should the STL be used with user defined types?' and the second is
'Are there any good books covering this?'</p>
<p>My choice of book after mastering Ammeraal's <i class=
"citetitle">STL for C++ programmers is Generic Programming and the
STL</i> by Matt Austern.</p>
<p>That is my answer to the second question, so how about the first
one?</p>
<p>I think the answer to that is simple as well. As long as you
ensure that your UDT is what I call a value type you can use it
almost exactly like a built-in type. The only caveat is that some
STL algorithms (e.g. <tt class="function">sort</tt>) and
collections (such as <tt class="classname">map</tt>) when used in
their default form require the comparison operators defined for
your type. Note that I said by default; you are always at liberty
to provide a comparison function explicitly when instantiating the
template.</p>
<p>Now let me back track and summarise what I mean by a value type.
By a value type I mean any type that has strict public copy
semantics. That means that the user has not made either the copy
constructor or the copy assignment operator private or protected.
Furthermore, either there are publicly defined versions with strict
copy semantics (unlike <tt class="classname">auto_ptr</tt>, for
example, where the copy process changes the object being copied),
or it is possible for the compiler to generate suitable functions
(i.e. no const qualified data members and no reference data
members).</p>
<p>The single largest cause of error in using the STL is applying
it to object or polymorphic types (and a common and serious design
error is making polymorphic types publicly copyable and/or copy
assignable). The STL is designed to work with values. If you want
collections of objects use 'handles', pointers or smart pointers
that can be safely provided with strict copy semantics.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e67" id="d0e67"></a>From John
Skelton <tt class="email">&lt;<a href=
"mailto:John_Skelton@compuserve.com">John_Skelton@compuserve.com</a>&gt;</tt></h2>
</div>
<p>In the answer you give in June's EXE, you say that:</p>
<pre class="programlisting">
  uppercase(hello).data
</pre>
<p>is exactly equivalent to:</p>
<pre class="programlisting">
  (&amp;uppercase(hello))-&gt;data
</pre>
<p>Please can you point me to a reference to confirm that? I've the
ANSI X3.159-1989 standard, and also K&amp;R (1st &amp; 2nd Eds) and
Harbison &amp; Steele (2nd Ed) but I've been unable to convince
myself that you're right. Actually, I think you're wrong as the
nearest ref I could find was about (&amp;E)-&gt;MOS but that's not
phrased in a way that would support what you say...</p>
<p>In case you've only a ref. in the ISO std, I hope it's as like
the ANSI std as I've seen it said. &lt;g&gt;</p>
<p>The C9X draft (or whatever it is) explicitly says the C you used
is allowed, but it doesn't make sense that you were referring to
C9X so I'm ignoring that.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e86" id="d0e86"></a>Response from
Francis Glassborow</h3>
</div>
<p>I think there are a number of very subtle issues floating around
here. But let me start with the simple one. Consider:</p>
<pre class="programlisting">
struct X { char data[10] };
typedef X X;  // make X a typename even in C
int main(){
X item;
...
}
</pre>
<p>Now I hope we all agree that (until we introduce C++ operator
overloading for &amp; and -&gt;) <tt class="literal">item.data</tt>
and <tt class="literal">(&amp;item)-&gt;data</tt> are required to
be equivalent. I think (hope) that that is simple and clear.</p>
<p>Now the next question is to consider the case when the
<span class="structname">struct X</span> object is an unnamed
return value from a function. Presumably <tt class=
"literal">uppercase(hello).data</tt> means apply the dot operator
to the returned value. So the next question is to what is the
address operator being applied in (<tt class=
"literal">&amp;uppercase(hello)</tt>), the function or the returned
value. A quick check of precedence reveals that the function
operator has higher precedence so the &amp; must be applied to the
result of evaluating <tt class="literal">uppercase(hello)</tt>. In
addition the unary &amp; can only be applied to a function
designator or to an lvalue so (<tt class=
"literal">&amp;uppercase(hello)</tt>) is ill-formed. Now the
question is whether <tt class="literal">uppercase(hello).data</tt>
is also ill-formed (which is what I was originally contending). Let
me rephrase that, are you allowed to apply the dot operator to an
rvalue? The answer to that question is 'yes' by deduction from the
standard (in 6.3.2.3 it states that 'The value is that of the named
member, and is an lvalue if the first expression is an
lvalue').</p>
<p>So it seems that John is indeed correct (which surprised me
because it breaks what I had always regarded as a required
equivalence). However I think we still have a problem. According to
this <tt class="literal">uppercase(hello).data</tt> must be an
rvalue of type <tt class="type">char[10]</tt>. But C does not
support array rvalues because there is nothing to decay to a
pointer (rvalues do not have location).</p>
<p>The original context was whether <tt class=
"literal">printf(&quot;%s&quot;, uppercase(hello).data);</tt> was
well-formed. I think the answer is no, not because you cannot pass
<tt class="literal">struct</tt>s by value - you can - but because
you cannot then handle the array data when you try to extract it
from the cocoon of its encapsulating struct.</p>
<p>I would welcome opinions from other language lawyers.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
