    <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  :: What's in a Namespace?</title>
        <link>https://members.accu.org/index.php/articles/713</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 + CVu Journal Vol 16, #6 - Dec 2004</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/c77/">CVu</a>

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

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+99/">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;What's in a Namespace?</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 December 2004 13:16:09 +00:00 or Fri, 03 December 2004 13:16:09 +00:00</p>
<p><strong>Summary:</strong>&nbsp;<p>In this article I will visit the mechanics of namespaces and anonymous namespaces and explain how they are used to solve some of the problems associated with linking C++ programs. Then I will move on to explain how they can also be used to provide context.</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>In my experience most C++ developers have heard about
namespaces. Most of them understand what namespaces are for and the
problems they solve. Some even make use of them!</p>
<p>Namespaces can be used for more than preventing name clashes. In
this article I will visit the mechanics of namespaces and anonymous
namespaces and explain how they are used to solve some of the
problems associated with linking C++ programs. Then I will move on
to explain how they can also be used to provide context.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e24" id="d0e24"></a>What are
Namespaces?</h2>
</div>
<p>The C++ standard has the following description of
namespaces:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>7.3.0.1 A namespace is an optionally-named declarative region.
The name of a namespace can be used to access entities declared in
that namespace; that is, the members of the namespace. Unlike other
declarative regions, the definition of a namespace can be split
over several parts of one or more translation units.</p>
</blockquote>
</div>
<p>This tells you what a namespace is, but not what one is used
for. Consider the following example:</p>
<p>You are writing a COM object that is going to be used to split
an input file into a number of output files. For maximum ease of
testability and performance you write the actual file processing
code in standard C++ and wrap it in a Fa&ccedil;ade [<a href=
"#Fa%C3%A7ade">Fa&ccedil;ade</a>] called <tt class=
"classname">FileSplitter</tt>. A COM object can then be written to
wrap the file processing <tt class="classname">FileSplitter</tt>
class. The COM object provides an interface that forwards to the
file processing <tt class="classname">FileSplitter</tt> class.</p>
<p>The COM object client has no knowledge that the COM object is
actually just a wrapper, and has no need to know. As far as the
client is concerned the COM object is the file splitter. Therefore
the obvious name for the COM object class is also <tt class=
"classname">FileSplitter</tt> (with <tt class=
"classname">IFileSplitter</tt> the obvious name for the
interface).</p>
<p>Having two classes with the same fully qualified name in a C++
program is not permitted. The solution is to introduce namespaces.
From Microsoft Visual C++ 7.0 onwards, all COM classes are placed
in the ATL namespace (in earlier versions COM classes were required
to be in the global namespace). However, although I will use the
ATL namespace in this article; and putting COM objects in the ATL
namespace is a Microsoft convention, using the name of a technology
for a namespace is not usually good practice as it does not provide
the right sort of context. For example it would not be sensible or
useful to group together all abstract base classes or all classes
that implement a recognized pattern.</p>
<p>Due to the limited scope of this example the name for the
namespace containing the file processing <tt class=
"classname">FileSplitter</tt> class is not clear. However, an
appropriate name might be something like <tt class=
"classname">Process</tt>, as the class performs the actual
processing of files within the program:</p>
<pre class="programlisting">
// filesplitter.h

namespace Process {
  class FileSplitter {
    ...
  };
}
</pre>
<p>The file processing <tt class="classname">FileSplitter</tt>
class can then be used by fully qualifying its name in the COM
class:</p>
<pre class="programlisting">
// filesplitter_com.h

#include &quot;filesplitter.h&quot;

namespace ATL {
  class FileSplitter : public IFileSplitter {
    ...
  private:
    Process::FileSplitter impl_;
  };
};
</pre>
<p>Both classes can now happily coexist in the same program,
despite the fact that they have the same name, as they are both in
different namespaces. The namespaces also help to make maintenance
easier by providing local context for each class.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e77" id="d0e77"></a>What are
Anonymous Namespaces?</h2>
</div>
<p>The C++ standard has the following to say about anonymous (or
unnamed) namespaces:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>7.3.1.1 An unnamed-namespace-definition behaves as if it were
replaced by</p>
<pre class="programlisting">
namespace unique{ /* empty body */ }
using namespace unique;
namespace unique { namespace-body }
</pre>
<p>where all occurrences of unique in a translation unit are
replaced by the same identifier and this identifier differs from
all other identifiers in the entire program. (Although entities in
an unnamed namespace might have external linkage, they are
effectively qualified by a name unique to their translation unit
and therefore can never be seen from any other translation
unit.)</p>
</blockquote>
</div>
<p>This is an even less useful description than the one for regular
namespaces. Bjarne Stroustrup has the following to say about
unnamed namespaces in The C++ Programming Langue [<a href=
"#">TCPPPL</a>]:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>It is often useful to wrap a set of declarations in a namespace
simply to protect against the possibility of name clashes. That is,
aim to preserve locality of code rather than to present an
interface for users...</p>
<p>In this case we can simply leave the namespaces without a
name...</p>
<p>Clearly, there has to be some way of accessing members of an
unnamed namespace from the outside. Consequently, an unnamed
namespace has an implied using-directive...</p>
<p>...In particular unnamed namespaces are different in different
translation units. As desired, there is no way of naming a member
of an unnamed namespace from another translation unit.</p>
</blockquote>
</div>
<p>This gets much closer to what an anonymous namespace is for, but
is still not as clear as it could be. Mark Radford was kind enough
to supply me with the following description and examples of the use
of anonymous namespaces:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>Designers of C++ programs often encounter a need for some
declarations to have Translation Unit (TU) scope. For example,
consider the encapsulation of database access using SQL: it may
well make sense for the SQL strings to be encapsulated within the
TU in which the database access is implemented.</p>
<p>Having declared identifiers for string constants within a TU,
the designer has another issue to resolve: what if the same
identifier is used in another TU? Without support from the C++
language, it may not be possible to guarantee avoiding this
situation, without telling other people what identifiers have been
used. Putting it another way: without language support, such
encapsulated identifiers are not really encapsulated.</p>
<p>In early C++ the solution was one inherited from C: declare
identifiers as static to give them internal linkage. However,
already this has the drawback of overloading the keyword static.
Also, as the language evolved and templates were added, it became
apparent there was another drawback: identifiers with internal
linkage could not be template arguments.</p>
<p>To resolve the above issues, a more C++-centric solution was
devised - the unnamed namespace, or as it tends to be called in
more common parlance, the &quot;anonymous namespace&quot;. Identifiers
declared in the anonymous namespace have external linkage (and can
be used as template arguments), but are accessible only in the TU
in which they are declared. The mechanism by which this is achieved
is implementation dependent, but a popular approach is the use of a
scheme where the compiler mangles the identifier name with that of
the TU.</p>
</blockquote>
</div>
<p>The descriptions in the C++ standard and The C++ Programming
Language, together with the comments from Mark Radford cover the
importance and uses of anonymous namespaces well. Not only do they
prevent name clashes, they also provide context. The reader of a
source file (.cpp) knows that anything located within the anonymous
namespace is only intended for use within that translation unit and
no other.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e116" id="d0e116"></a>Namespaces
Provide Context</h2>
</div>
<p>Namespaces do not only provide solutions to the problems
associated with linking C++ programs. They can also provide context
that helps a developer determine a class's, a function's or a
variable's position and purpose within a program just from looking
at a single source file.</p>
<p>Before describing the mechanics of namespaces in The C++
Programming language, Stroustrup has the following to say about
them:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>A namespace is a mechanism for logical grouping. That is, if
some declarations logically belong together according to some
criteria, they can be put in a common namespace to express the
fact&hellip;.</p>
</blockquote>
</div>
<p>So, namespaces are also about grouping related elements of a
program together. The file splitter example above can be expanded
to demonstrate this. Suppose your program not only splits files,
but can also merge them.</p>
<p>Again, the standard C++ processing code should be wrapped in a
fa&ccedil;ade class (<tt class="classname">FileMerger</tt>) and
should be separate from the COM class. The processing class
processes files and therefore belongs in the <tt class=
"literal">Process</tt> namespace along with <tt class=
"classname">Process::FileSplitter</tt>.</p>
<pre class="programlisting">
// filesplitter.h
namespace Process {
  class FileSplitter {
    ...
  };
}
// filemerger.h
namespace Process  {
  class FileMerger {
    ...
  };
}
</pre>
<p>The <tt class="classname">FileMerger</tt> COM class, of course,
goes into the <tt class="literal">ATL</tt> namespace with
<tt class="classname">ATL::FileSplitter</tt>.</p>
<p>Elements that are grouped together by a namespace share a
context. Equally, when you look at a single class, function or
variable declaration you know what context it is in from its
namespace.</p>
<p>For example, you could open any source or header file from the
example above and be looking at a <tt class=
"classname">FileSplitter</tt> or <tt class=
"classname">FileMerger</tt> class and know immediately whether it
was a file processing class or a COM class, just from its
namespace. This is a significant maintenance advantage as you would
not have to go searching through other source and header files
trying to determine the context of the file you had just
opened.</p>
<p>There are, of course, other ways of providing this context.
Some, such as directory structure, complement the use of namespaces
very well, but is a subject beyond the scope of this article.</p>
<p>Appending File to the front of <tt class=
"classname">FileSplitter</tt> and <tt class=
"classname">FileMerger</tt> suggests that there can be other types
of splitters and mergers within the context of the program.
Otherwise, they may as well just be called <tt class=
"classname">Splitter</tt> and <tt class="classname">Merger</tt>. In
the example presented so far that would be perfectly
reasonable.</p>
<p>However, now consider that as well as splitting complete files,
record by record, the records themselves are split in some way. The
logical name for a class that splits a record is <tt class=
"classname">RecordSplitter</tt>. This introduces a new context and
should really introduce a new namespace:</p>
<pre class="programlisting">
// recordsplitter.h
namespace Process {
  namespace Record {
    class Splitter {
      ...
    };
  }
}
</pre>
<p>If a record merging class is introduced into the program that
too would go into the <tt class="literal">Record</tt> namespace.
The file processes should also be placed in a nested namespace:</p>
<pre class="programlisting">
// filesplitter.h
namespace Process {
  namespace File {
    class Splitter {
      ...
    };
  }
}

// filemerger.h

namespace Process {
  namespace File {
    class Merger {
      ...
    };
  }
}
</pre>
<p>This technique can of course be taken too far and is probably
overkill for this example, but I hope it shows the concept of
namespaces providing context.</p>
<p>What about anonymous namespaces? Do they provide context too?
Absolutely! Anonymous namespaces provide context within a
translation unit. As stated above, they tell you that the contents
of the anonymous namespace are only intended for use in the current
translation unit.</p>
<p>Consider the following example. You have a lookup table of
postcodes that are to be loaded from a database:</p>
<pre class="programlisting">
// Postcode.cpp

#include &quot;lookup\postcode.h&quot;

namespace PostcodeTools {
  namespace {
    const std::string postcodeSql
        = &quot;SELECT postcode FROM postcodes&quot;;
  }

  void Postcode::Load() {
    dbConn_-&gt;Execute( ... );
    ...
  }

  ...
}
</pre>
<p>There are a number of things that can be done with the
<tt class="varname">postcodeSql</tt> string. It could be a local
variable inside the load function, but it may be something that
changes if the database table moves or is renamed for some reason.
Therefore it should be as prominent as possible to make finding it
easy. This would suggest it should be brought out to namespace
scope so that it is near the top of the file. This opens up the
possibility of a name clash (although const variables actually have
internal linkage) as other translation units containing the
namespace <tt class="literal">PostcodeTools</tt>, could also have a
postcodeSql member also.</p>
<p>The obvious solution is to place <tt class=
"varname">postcodeSql</tt> in an anonymous namespace as shown. Even
though members of an anonymous namespace have external linkage,
they cannot clash with names declared in other translation units.
The anonymous namespace also tells you that the <tt class=
"varname">postcodeSql</tt> string is only intended for use within
the source file.</p>
<p>In this article I have examined the use of namespaces and
anonymous namespaces and the context provided by them. I hope I
have made a good case for their usage and that I have encouraged
readers to use namespaces more widely and for context as well as
for preventing name clashes.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e218" id=
"d0e218"></a>Acknowledgments</h2>
</div>
<p>Thank you to Adrian Fagg, Mark Radford, Phil Bass and Alan
Griffiths.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e223" id="d0e223"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Fa%C3%A7ade"></a>
<p class="bibliomixed">[Fa&ccedil;ade] Alan Shalloway, James J.
Trott, <span class="citetitle"><i class="citetitle">Design Patterns
Explained: A New Perspective on Object-oriented Design</i></span>,
ISBN: 0201715945</p>
</div>
<div class="bibliomixed"><a name="TCPPL" id="TCPPL"></a>
<p class="bibliomixed">[TCPPL] Bjarne Stroustrup, <span class=
"citetitle"><i class="citetitle">The C++ Programming Language,
Special Edition</i></span>, ISBN: 0201700735</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
