    <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  :: Letters: Encapsulate Context</title>
        <link>https://members.accu.org/index.php/journals/259</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>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #65 - Feb 2005 + Letters to the Editor</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/journals/">All</a>

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c147/">65</a>
                    (9)
<br />

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c186/">LettersEditor</a>
                    (132)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c147-186/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c147+186/">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;Letters: Encapsulate Context</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 12 February 2005 16:35:57 +00:00 or Sat, 12 February 2005 16:35:57 +00: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>Encapsulate
Context</h2>
</div>
<p>Mr. Griffiths,</p>
<p>When I read the title and abstract for the pattern, I thought it
might be really useful. Instead, this Pattern is simply a global
wrapped in a shroud. I would like to say, at the outset, that I do
believe there is a place for global variables (<tt class=
"classname">std::cout</tt> being an excellent example). I also like
the concept of contextually global variables - variables that are
global within a given context. To make my argument I offer the
following:</p>
<div class="orderedlist">
<ol type="A">
<li>
<p>There are two ways a variable becomes global within a program:
1) it is intentionally declared global, or 2) it is passed to every
function as a parameter.</p>
</li>
<li>
<p>The ENCAPSULATE CONTEXT pattern suggests that there is a single
variable that should be passed to all functions within a given
context, effectively making the variable global (via A.).</p>
</li>
<li>
<p>The ENCAPSULATE CONTEXT pattern as written, does not build a
context into the variable, but rather provides a global that can be
provided within context. There is nothing to define or constrain
the context under which variables within the structure should be
accessed.</p>
</li>
</ol>
</div>
<p>In short, the functionality provided by the ENCAPSULATE CONTEXT
pattern as written can be easily achieved by declaring some global
variables in a namespace that is shared by the functions that need
access to these variables. This &quot;solution&quot; is far cheaper, more
effective, and clearly delineates the level of coupling the
functions share, whereas the ENCAPSULATE CONTEXT solution as
written only serves to obfuscate the level of coupling, not
mitigate it.</p>
<p>Were I a sufficiently adept programmer, I would propose an
alternative to the ENCAPSULATE CONTEXT solution as written. Sadly,
my skills do not lie in this area and it would take me ages to
accomplish this task. At best I can offer some design
suggestions.</p>
<p>It strikes me that the container which &quot;collects data together&quot;
needs to do more than simply provide a pointer that can be passed
around on the parameter line. It should have a built in hierarchy
of contexts that allow functions to pass not simply the container,
but also information about the context under which the data should
be accessed. By providing the context (albeit dynamically) it is
possible to limit the scope of the variables that are accessible as
the context tree is traversed.</p>
<p>Taking the straw man stock exchange trading system that Allan
Kelly used, I would see the calls to the functions something like
this:</p>
<pre class="programlisting">
ProcessMarketTrade(msg,
               context-&gt;constrain(data_store
                                  || log));
MarketStore::Sell(msg,
                  context-&gt;constrain(log));
</pre>
<p>the <tt class="function">constrain</tt> function from within
<tt class="classname">context</tt> should look something like:</p>
<pre class="programlisting">
MarketContext * MarketContext::constrain(
                         ContextLevel level);
</pre>
<p>In this case, the parameters to <tt class=
"function">context.constrain()</tt> define the highest point in the
hierarchies of data stored within the context to which the function
should have access and the call returns a pointer to a variable
which has been so constrained (I can envision several objections to
this particular implementation, but I hope that the idea is clear).
The hierarchy within the <tt class="classname">context</tt>
container might look something like:</p>
<pre class="programlisting">
config_data
  parms
app_data
  data_store
run_time_data
  log
    error_log
    transaction_log
</pre>
<p>Within the hierarchy there might be variables associated with
that point in the hierarchy ... in run_time_data/log/error_log
there might be a lock that is used to lock the file while a set of
error messages is being written, the file pointer, etc. It is worth
noting that you shouldn't be able to pass a context above your
current context. Thus, if you had received the container with the
context level of <tt class="literal">log</tt> you would be able to
pass on <tt class="literal">log</tt>, <tt class=
"literal">error_log</tt>, or <tt class=
"literal">transaction_log</tt>, but not <tt class=
"literal">parms</tt> or <tt class="literal">run_time_data</tt>.
Maybe all of this could be done with templates somehow.</p>
<p>This would encapsulate the context and refine how generally
global variables might be accessed. A function with access to &quot;log&quot;
might not have access to &quot;parms&quot; and this would help with
decoupling. It strikes me that this is still a long-winded and
complicated way to achieve the same thing as globals within a
namespace. Given your concerns, however, that it might be good to
be able to recover from the state in which you find the application
rather than simply rewriting it so that such coupling is either not
needed or explicit, this may be a reasonable approach. Again, I
wish I had the necessary skills to adequately program such a
structure, but my &quot;back of the envelope&quot; efforts have not come to
fruition and I'm wondering if I have grasped the whole of the
problem or just a piece.</p>
<p>As a final note, I would like to say that the article was clear,
well written, and was on topic for Overload. I do feel that the
solution given represents a &quot;bad programming practice&quot; but I
readily concede to those more learned in this area then myself. I
also think that the solution given is a very complicated way to
ignore namespaces for no particular gain and some loss.</p>
<p>Sincerely,</p>
<p>William Fishburne</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e96" id="d0e96"></a>Allan's
Reply</h2>
</div>
<p>Dear Editor,</p>
<p>I'm rather surprised by the amount of attention my little
pattern, ENCAPSULATE CONTEXT, in Overload 63 has generated.
However, I regard this as a good thing.</p>
<p>I'm a little disappointed that I was not given the opportunity
to respond to Phil Bass's points in the same issue of Overload as
his letter appeared. Phil's technical points are all valid, the
problem is: how do we balance all these forces? That is the problem
that ENCAPSULATE CONTEXT addresses.</p>
<p>Phil is also concerned about the resulting coupling. I too am
concerned about this and would draw his attention to the Solution
and Consequences sections. These deal with some of the problems
which can arise when this pattern is used - or misused. This paper
does not claim to be a solution to every programming problem. Like
any other pattern this one may be useful sometimes and not others.
If a system can be partitioned to avoid this problem then great, if
not, then this pattern has a use. Unlike many other patterns this
one knows its limitations and highlights these to the reader.</p>
<p>Readers must decide whether this pattern stands or falls.
However I am more concerned about a non-technical point Phil makes
in his letter. He states &quot;mention of Kevlin Henney, Frank Buschmann
and EuroPLoP gives the article an unwarranted air of authority.&quot;
Let me say that these people are mentioned not to aggrandise the
pattern or myself but to acknowledge their assistance in creating
the pattern. I am most grateful to these people - and the others
mentioned - for all their help. It is in the culture of the
patterns community that such assistance is acknowledged.</p>
<p>In no way did I hide anything from these people, there was no
attempt to pull the wool over their eyes and slip a dodgy pattern
past them. The final choice of words may have been mine but this
pattern wouldn't be what it is without the review and comments of
others.</p>
<p>These names were not mentioned to create a false authority for
the pattern. If the pattern has authority it comes not from mention
of these names but form the fact that others have reviewed it on
multiple occasions. (In fact, I find it hard to think of any other
piece of writing in Overload that has been reviewed as much as this
- and that was before the Overload editorial board got to see
it.)</p>
<p>Moving onto the comments from William Fishburne. First, let me
encourage William to write up his thoughts. I'm sure his skills are
up to the job.</p>
<p>Reading William's comments I take two main points. Firstly his
suggestion to use a namespace as part of the solution. What he
describes sounds like the MONOSTATE pattern - otherwise known as
THE BORG. This pattern has its uses but - as the alternative name
suggests - it too has problems.</p>
<p>The second solution is to constrain the parameters passed to a
function. This may well be a solution in some contexts, but in the
context taken by ENCAPSULATE CONTEXT it is not. In ENCAPSULATE
CONTEXT we wish the caller to remain ignorant of what is being
passed down. This is a deliberate selective ignorance. While this
approach introduces compile time coupling the coupling is less than
would be introduced by either global variables or an extra
parameter in the function signature.</p>
<p>We could remove this coupling were we to use dynamic members
within the context, this is the approach taken by Patow and Lyaret
in PARAMETER BLOCK pattern (another pattern presented at EuroPLoP
2003.)</p>
<p>Both Phil and William describe my example as a straw man. To
some degree this is true, like any example it is an abstraction
which ignores some elements, however, I can assure them it is not a
straw man example.</p>
<p>The example given comes from a very real system. Two
explanations are therefore possible:</p>
<div class="variablelist">
<dl>
<dt><span class="term">Option #1:</span></dt>
<dd>
<p>A better design was possible that would have avoided this design
situation. Undoubtedly other designs where possible, would other
designs have avoided this problem? I don't know. I do know that
this solution troubled me as it emerged; this was the trigger for
deeper investigation and ultimately writing the pattern.</p>
<p>More importantly, like any design this evolved, knowing what one
knows at the end of a design one might not always chose the same
design again. However, I am convinced that given the constraints at
the time (knowledge of domain, time to market, programming
interfaces, experience, etc.) this was the best design
possible.</p>
</dd>
<dt><span class="term">Option #2:</span></dt>
<dd>
<p>Simply that I am a poor designer. I will let readers decide this
for themselves but I will note that others have come to similar
conclusions in similar circumstances.</p>
</dd>
</dl>
</div>
<p>Finally, I welcome this debate, it's good to hear other views
and it demonstrates that software development is not black and
white, different opinions exists and always will. I look forward to
hearing more comments and opinions.</p>
<p>Allan Kelly <tt class="email">&lt;<a href=
"mailto:allan@allankelly.net">allan@allankelly.net</a>&gt;</tt></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e146" id="d0e146"></a>Alan's reply
to Allan</h2>
</div>
<p>Allan is right: if a &quot;letter to the editor&quot; had expressed the
views that Phil propounded then I would have sought a response from
the author. For that I have to apologise, both to Allan and to the
readership. Sorry.</p>
<p>Phil's comments caused discussion within the editorial team -
not so much because of the views expressed, but how to deal with
them. Given Phil's position as one of Overload's advisors his
comments needed different treatment to those of most
correspondents. Especially as the main point was regarding
editorial policy: should material that an advisor has concerns
about be published without warning?</p>
<p>My experience with the solution Allan proposes is that a number
of problems experienced on the project that I employed it on were
resolved without unexpected effects. Hence, I didn't feel it
appropriate to give any warning beyond those Allan himself
provides.</p>
<p>I trust that Overload readers are sufficiently sophisticated to
make up their own minds about both the validity of Allan's pattern
paper and also about Phil's concerns regarding publishing it.</p>
<p>Alan Griffiths (editor)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e159" id="d0e159"></a>Developing a
Pattern</h2>
</div>
<p>Dear Editor,</p>
<p>I was a little surprised and more than a little worried by the
comments made by Phil Bass (included in the editorial of Overload
64) on the ENCAPSULATE CONTEXT pattern by Allan Kelly (published in
Overload 63). They were quite strongly against Allan's article,
which is a personal preference anyone is allowed to express, but
for reasons that I felt were ungrounded, and which undermined the
value of those comments as a review.</p>
<p>The first concern I had was with Phil's question over the
validity of the pattern. Given Phil's experience and the way that
he has approached design in his articles, this is surprising. The
solution outlined in the pattern is both valid and good for the
problem it addresses, and a standard tool in the toolkit of
experienced OO developers. For the record, and to assuage Phil's
concerns, it can be found in a number of well-designed systems; it
can also be found missing in several systems that are not so well
designed, where instead single, fixed points of contact (globals,
SINGLETONS, MONOSTATES, etc) are employed or long, unstable and
tedious argument lists are passed around.</p>
<p>Unifying sets of distinct items, such as all or part of an
argument list, that are bound by common use or an invariant is one
of the diverse range of techniques OO developers use to identify
object types. Phil considers such a use to be trivial, which
appears to go against much of the accumulated wisdom on identifying
stable elements in a design. Such normalisation is a common and
accepted practice, whether carried out explicitly or
intuitively.</p>
<p>The second concern I had is concerned with a fundamental part of
the pattern concept. A pattern is not an unconditional piece of
design advice, a blanket recommendation that covers all designs.
Its applicability is very much dependent on context and the
acceptability of trade-offs involved in applying it. A robustly
written pattern will make clear that it is not a panacea,
publicising its benefits, liabilities and alternatives quite
openly. This is a point that Mark Radford made clearly and well in
his editorial of Overload 63. Hence the reason for my surprise:
Phil's comments suggest that he views the pattern as unconditional
design advice that has neither a discussion of consequences nor a
discussion of techniques involved in applying it, such as
partitioning the context. I don't believe that this is the way that
Phil actually views patterns, but that is the message that comes
across in this instance.</p>
<p>Allan's pattern is quite clear in its caution, making explicit
the many design decisions and alternative paths that would lead to
or away from encapsulating the execution context of an object. Phil
does not seem to have picked up on some of the other points made in
the paper, such as partitioning the execution context. He proceeds
to misapply the pattern in his discussion, and then claim that it
is the pattern that is broken and not his reading of it. It is
important to recognise that the pattern does not unconditionally
propose that there be only one context type or context object (or,
to pick up a previous point a conditional application, that context
objects are needed in all system designs). To claim that the
system's coupling will rise if Encapsulated Context is applied both
misses and makes the point: employ the pattern to reduce the
coupling rather than increase it; if that doesn't work, do it
differently or do something else. If Phil does not wish to apply
the pattern, I have no problem with that; if he has been unable to
evaluate it on its own terms and is cautioning others that it
should not be used at all, I question that.</p>
<p>The first two concerns are technical in character, and are the
stuff of lively technical debate, whatever views we hold. However,
the third concern I had was more ad hominem in nature. Phil's claim
that &quot;the mention of Kevlin Henney, Frank Buschmann and EuroPLoP
gives the article an unwarranted air of authority&quot; is an
inappropriate claim that potentially insults all those involved -
Allan, Frank, me and the EuroPLoP workshop participants who offered
Allan feedback on his paper.</p>
<p>In his prologue and his acknowledgements section Allan offers an
insight into the history and evolution of the paper. In recognising
that it has evolved, and the journey taken so far, he mentions
those who have contributed in some way to the paper. That is part
of telling the story of the paper, but it is also why
&quot;acknowledgements&quot; sections are so called. Our names were not
picked out of thin air to aggrandise Allan's work: we offered Allan
feedback in one form or another. It is generally considered polite
to acknowledge such contributions. Such acknowledgements are also
part of the cultural expectation for pattern papers.</p>
<p>It is perhaps worth understanding a little more about the
process that surrounds the reviewing of many pattern papers. A
shepherd is someone who offers comments structured as iterative
feedback with dialogue, with the goal of giving the author a
different insight into their paper and the means to improve it.
This is the role that I took on voluntarily when the pattern
originally came up in discussion in 2002. At this point Allan was
working on the paper without a distinct publication goal in mind.
Allan decided to submit it to EuroPLoP 2003, and at this point
Frank Buschmann took on the role of the shepherd. Papers are not
accepted for the PLoP family of conferences without some amount of
shepherding and acceptance by the shepherd. The shepherding is
intended to improve the paper to a point where it can be opened to
further structured feedback at the conference in the form of a
workshop, which is where Allan received further suggestions for
improvement. Publication in the EuroPLoP proceedings is not a mark
of perfection, but is a visible outcome of the reviewing process,
and to reference it is a statement of fact rather than an appeal to
authority.</p>
<p>Given the depth and breadth of the review process, it seems only
good manners to include a list of acknowledgements. And, given the
common characterisation of a pattern paper as a work that is always
in progress, it is possible that Allan will take Phil's comments on
board and address them in some way. In such a case, it is also
likely that Phil's name will appear in the list of
acknowledgements, because in one way or another he will have
contributed to improving the paper.</p>
<p>Kevlin Henney <tt class="email">&lt;<a href=
"mailto:kevlin@curbralan.com">kevlin@curbralan.com</a>&gt;</tt></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e186" id="d0e186"></a>Phil's
Response</h2>
</div>
<p>Dear Editor,</p>
<p>The only comment I really wish to make is that I apologise
unreservedly to Allan, Kevlin and anyone else who feels insulted by
my remarks. When I said that &quot;the mention of Kevlin Henney, Frank
Buschmann and EuroPLoP gives the article an unwarranted air of
authority&quot; I chose my words badly. I still believe Allan's article
over-states the value of this &quot;pattern&quot;, but I never intended to
question anyone's integrity.</p>
<p>The real technical issue, here, lies with Allan's initial
premise that &quot;A system contains data, which must be generally
available to divergent parts of the system&quot;. That is a description
of a problem. ENCAPSULATE CONTEXT is a sticking plaster that can be
applied if you wish, but it doesn't begin to tackle the problem
itself. What we should be doing is analysing the system's design
with a view to removing (as far as possible) the need for such
data.</p>
<p>Phil Bass <tt class="email">&lt;<a href=
"mailto:phil@stoneymanor.demon.co.uk">phil@stoneymanor.demon.co.uk</a>&gt;</tt></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e199" id="d0e199"></a>C++ Lookup
Mysteries</h2>
</div>
<p>Dear Editor,</p>
<p>Sven Rosvall's &quot;C++ Lookup Mysteries&quot; in Overload 63 couldn't
have been better timed as it provided a solution to a problem I had
been struggling with - a test harness that failed to compile after
a new feature was added to the main product because of C++'s
non-intuitive name lookup rules.</p>
<p>The problematic code, trimmed to the minimum to illustrate the
problem, was:</p>
<pre class="programlisting">
template&lt;class C&gt;
void DebugPrint(const string&amp; description,
                const C&amp; container) {
  cout &lt;&lt; description &lt;&lt; &quot;\n&quot;;
  // some stuff
  copy(container.begin(), container.end(),
       ostream_iterator&lt;typename C::iterator
                    ::value_type&gt;(cout, &quot; &quot;));
  cout &lt;&lt; endl;
  // some other stuff
}
</pre>
<p>This works fine for standard containers containing either
built-in types or user defined types that define an output operator
in the same namespace as the type is defined. The code that broke
the test harness was a standard container of <tt class=
"classname">std::pair</tt>. The obvious solution, defining
<tt class="methodname">operator&lt;&lt;(std::pair)</tt> in the test
harness namespace, didn't work because the compiler cannot &quot;see&quot;
this definition. The problem is that <tt class=
"methodname">operator&lt;&lt;</tt> is already defined (for the
built-in types) in <tt class="literal">namespace std</tt> and masks
my definition, as Sven explains &quot;Firstly, the nearest enclosing
namespace is searched for 'entities' with the same name.
<span class="emphasis"><em>Note that as soon as a name is found the
search stops</em></span>&quot; (my italics). C++ name lookup says that
the only place that the compiler will look for <tt class=
"methodname">operator&lt;&lt;(std::pair)</tt> is in <tt class=
"literal">std</tt>.</p>
<p>Ah, so all I have to do is define it in <tt class=
"literal">std</tt>:</p>
<pre class="programlisting">
namespace std {
  template&lt;class T1, class T2&gt;
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os,
                  const std::pair&lt;T1,T2&gt;&amp; p) {
    return os &lt;&lt; &quot;(&quot; &lt;&lt; p.first
              &lt;&lt; &quot;,&quot; &lt;&lt; p.second &lt;&lt; &quot;)&quot;;
  }
}
</pre>
<p>except that adding declarations or definitions to <tt class=
"literal">namespace std</tt> is undefined behaviour according to
the standard (Clause 17.4.3.1).</p>
<p>Of course the name lookup will find <tt class=
"methodname">operator&lt;&lt;(pair)</tt> in the test harness
namespace for a <tt class="classname">pair</tt> also in that
namespace. The standard fully defines <tt class=
"classname">std::pair</tt> (Clause 20.2.2) so I can copy the source
code to define my own <tt class="classname">pair</tt> (in my
workspace) and expect identical behaviour. Although legal, this has
a number of problems:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>It breaks the rule of least surprise. A future maintainer may
wonder why there are two identical <tt class="classname">pair</tt>s
in different namespaces.</p>
</li>
<li>
<p>It requires the main product source code to use this new
<tt class="classname">pair</tt>, and thus adds a dependency on the
test harness code.</p>
</li>
<li>
<p>Although the two pairs are binary compatible, they are not
interchangeable in source code without considerable scaffolding,
and even then not fully.</p>
</li>
</ul>
</div>
<p>To force the name lookup to find my <tt class=
"methodname">operator&lt;&lt;(std::pair)</tt> without duplicating
<tt class="classname">std::pair</tt>, I took Sven's wrapper class,
<tt class="classname">PrintSpannerNameAndGap</tt>, and made it into
a template class and output function:</p>
<pre class="programlisting">
template&lt;class T&gt;
class osformatter {
public:
  osformatter(const T&amp; t) : t_(t) {}

  void print(std::ostream&amp; os) const {os &lt;&lt; t_;}
private:
  const T &amp; t_;
};

template&lt;class T&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os,
                    const osformatter&lt;T&gt;&amp; f) {
  f.print(os);
  return os;
}
</pre>
<p>and changed the line in the debug function to use it:</p>
<pre class="programlisting">
  copy(container.begin(), container.end(),
    ostream_iterator&lt;osformatter&lt;typename
       C::iterator::value_type&gt; &gt;(cout, &quot; &quot;));
</pre>
<p>This now works for all built-in types and any user defined types
that define an <tt class="methodname">operator&lt;&lt;</tt> in the
same namespace.</p>
<p>Now it is possible to write a specialisation of <tt class=
"classname">osformatter</tt> for any type that does not support the
output operator or for which we want some special formatting, for
example, fixed precision doubles:</p>
<pre class="programlisting">
class osformatter&lt;double&gt; {
public:
  osformatter(const double&amp; d) : d_(d) {}
  void print(std::ostream&amp; os) const {
    int p=os.precision();
    os.precision(4);
    os &lt;&lt; d_;
    os.precision(p);
  }
private:
  const double&amp; d_;
};
</pre>
<p>I can now apply the same specialisation to <tt class=
"classname">std::pair</tt>, which, being a template itself, needs a
template declaration for the types contained within it:</p>
<pre class="programlisting">
template&lt;class T1, class T2&gt;
class osformatter&lt;std::pair&lt;T1,T2&gt; &gt; {
public:
  osformatter(const std::pair&lt;T1,T2&gt;&amp; p)
                                     : p_(p) {}
  void print(std::ostream&amp; os) const {
    os &lt;&lt; &quot;(&quot; &lt;&lt; p_.first
       &lt;&lt; &quot;,&quot; &lt;&lt; p_.second &lt;&lt; &quot;)&quot;;
  }
private:
  const std::pair&lt;T1,T2&gt;&amp; p_;
};
</pre>
<p>This now compiles because the <tt class=
"classname">std::pair</tt> output code is contained in <tt class=
"classname">osformatter</tt>, and thus explicitly called, and so is
no longer dependent on the name lookup rules.</p>
<p>This not only solved my name lookup problem but provided a nice
way of changing the default output format of built-in types when
using <tt class="function">copy</tt>.</p>
<p>Regards,</p>
<p>Mark Easterbrook</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
