1 |
742 |
jeremybenn |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
2 |
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
3 |
|
|
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Namespaces</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"/><meta name="keywords" content=" ISO C++ , library "/><meta name="keywords" content=" ISO C++ , runtime , library "/><link rel="home" href="../index.html" title="The GNU C++ Library"/><link rel="up" href="using.html" title="Chapter 3. Using"/><link rel="prev" href="using_macros.html" title="Macros"/><link rel="next" href="using_dynamic_or_shared.html" title="Linking"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Namespaces</th></tr><tr><td align="left"><a accesskey="p" href="using_macros.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Using</th><td align="right"> <a accesskey="n" href="using_dynamic_or_shared.html">Next</a></td></tr></table><hr/></div><div class="section" title="Namespaces"><div class="titlepage"><div><div><h2 class="title"><a id="manual.intro.using.namespaces"/>Namespaces</h2></div></div></div><div class="section" title="Available Namespaces"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.all"/>Available Namespaces</h3></div></div></div><p> There are three main namespaces.
|
4 |
|
|
</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>std</p><p>The ISO C++ standards specify that "all library entities are defined
|
5 |
|
|
within namespace std." This includes namespaces nested
|
6 |
|
|
within <code class="code">namespace std</code>, such as <code class="code">namespace
|
7 |
|
|
std::tr1</code>.
|
8 |
|
|
</p></li><li class="listitem"><p>abi</p><p>Specified by the C++ ABI. This ABI specifies a number of type and
|
9 |
|
|
function APIs supplemental to those required by the ISO C++ Standard,
|
10 |
|
|
but necessary for interoperability.
|
11 |
|
|
</p></li><li class="listitem"><p>__gnu_</p><p>Indicating one of several GNU extensions. Choices
|
12 |
|
|
include <code class="code">__gnu_cxx</code>, <code class="code">__gnu_debug</code>, <code class="code">__gnu_parallel</code>,
|
13 |
|
|
and <code class="code">__gnu_pbds</code>.
|
14 |
|
|
</p></li></ul></div><p> A complete list of implementation namespaces (including namespace contents) is available in the generated source <a class="link" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaces.html">documentation</a>.
|
15 |
|
|
</p></div><div class="section" title="namespace std"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.std"/>namespace std</h3></div></div></div><p>
|
16 |
|
|
One standard requirement is that the library components are defined
|
17 |
|
|
in <code class="code">namespace std::</code>. Thus, in order to use these types or
|
18 |
|
|
functions, one must do one of two things:
|
19 |
|
|
</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>put a kind of <span class="emphasis"><em>using-declaration</em></span> in your source
|
20 |
|
|
(either <code class="code">using namespace std;</code> or i.e. <code class="code">using
|
21 |
|
|
std::string;</code>) This approach works well for individual source files, but
|
22 |
|
|
should not be used in a global context, like header files.
|
23 |
|
|
</p></li><li class="listitem"><p>use a <span class="emphasis"><em>fully
|
24 |
|
|
qualified name</em></span> for each library symbol
|
25 |
|
|
(i.e. <code class="code">std::string</code>, <code class="code">std::cout</code>) Always can be
|
26 |
|
|
used, and usually enhanced, by strategic use of typedefs. (In the
|
27 |
|
|
cases where the qualified verbiage becomes unwieldy.)
|
28 |
|
|
</p></li></ul></div></div><div class="section" title="Using Namespace Composition"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.comp"/>Using Namespace Composition</h3></div></div></div><p>
|
29 |
|
|
Best practice in programming suggests sequestering new data or
|
30 |
|
|
functionality in a sanely-named, unique namespace whenever
|
31 |
|
|
possible. This is considered an advantage over dumping everything in
|
32 |
|
|
the global namespace, as then name look-up can be explicitly enabled or
|
33 |
|
|
disabled as above, symbols are consistently mangled without repetitive
|
34 |
|
|
naming prefixes or macros, etc.
|
35 |
|
|
</p><p>For instance, consider a project that defines most of its classes in <code class="code">namespace gtk</code>. It is possible to
|
36 |
|
|
adapt <code class="code">namespace gtk</code> to <code class="code">namespace std</code> by using a C++-feature called
|
37 |
|
|
<span class="emphasis"><em>namespace composition</em></span>. This is what happens if
|
38 |
|
|
a <span class="emphasis"><em>using</em></span>-declaration is put into a
|
39 |
|
|
namespace-definition: the imported symbol(s) gets imported into the
|
40 |
|
|
currently active namespace(s). For example:
|
41 |
|
|
</p><pre class="programlisting">
|
42 |
|
|
namespace gtk
|
43 |
|
|
{
|
44 |
|
|
using std::string;
|
45 |
|
|
using std::tr1::array;
|
46 |
|
|
|
47 |
|
|
class Window { ... };
|
48 |
|
|
}
|
49 |
|
|
</pre><p>
|
50 |
|
|
In this example, <code class="code">std::string</code> gets imported into
|
51 |
|
|
<code class="code">namespace gtk</code>. The result is that use of
|
52 |
|
|
<code class="code">std::string</code> inside namespace gtk can just use <code class="code">string</code>, without the explicit qualification.
|
53 |
|
|
As an added bonus,
|
54 |
|
|
<code class="code">std::string</code> does not get imported into
|
55 |
|
|
the global namespace. Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
|
56 |
|
|
<code class="code">using</code>-declarations can wrapped in macros that
|
57 |
|
|
are set based on autoconf-tests to either "" or i.e. <code class="code">using
|
58 |
|
|
std::string;</code> (depending on whether the system has
|
59 |
|
|
libstdc++ in <code class="code">std::</code> or not). (ideas from
|
60 |
|
|
Llewelly and Karl Nelson)
|
61 |
|
|
</p></div></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td align="left"><a accesskey="p" href="using_macros.html">Prev</a> </td><td align="center"><a accesskey="u" href="using.html">Up</a></td><td align="right"> <a accesskey="n" href="using_dynamic_or_shared.html">Next</a></td></tr><tr><td align="left" valign="top">Macros </td><td align="center"><a accesskey="h" href="../index.html">Home</a></td><td align="right" valign="top"> Linking</td></tr></table></div></body></html>
|