<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Namespaces</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.0" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="using.html" title="ChapterΒ 3.Β Using" /><link rel="prev" href="using_headers.html" title="Headers" /><link rel="next" href="using_macros.html" title="Macros" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Namespaces</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="using_headers.html">Prev</a>Β </td><th width="60%" align="center">ChapterΒ 3.Β Using</th><td width="20%" align="right">Β <a accesskey="n" href="using_macros.html">Next</a></td></tr></table><hr /></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.intro.using.namespaces"></a>Namespaces</h2></div></div></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.all"></a>Available Namespaces</h3></div></div></div><p> There are three main namespaces.</p><div class="itemizedlist"><ul type="disc"><li><p>std</p><p>The ISO C++ standards specify that "all library entities are definedwithin namespace std." This includes namespaces nestedwithin <code class="code">namespace std</code>, such as <code class="code">namespacestd::tr1</code>.</p></li><li><p>abi</p><p>Specified by the C++ ABI. This ABI specifies a number of type andfunction APIs supplemental to those required by the ISO C++ Standard,but necessary for interoperability.</p></li><li><p>__gnu_</p><p>Indicating one of several GNU extensions. Choicesinclude <code class="code">__gnu_cxx</code>, <code class="code">__gnu_debug</code>, <code class="code">__gnu_parallel</code>,and <code class="code">__gnu_pbds</code>.</p></li></ul></div><p> A complete list of implementation namespaces (including namespace contents) is available in the generated source <a class="ulink" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaces.html" target="_top">documentation</a>.</p></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.std"></a>namespace std</h3></div></div></div><p>One standard requirement is that the library components are definedin <code class="code">namespace std::</code>. Thus, in order to use these types orfunctions, one must do one of two things:</p><div class="itemizedlist"><ul type="disc"><li><p>put a kind of <span class="emphasis"><em>using-declaration</em></span> in your source(either <code class="code">using namespace std;</code> or i.e. <code class="code">usingstd::string;</code>) This approach works well for individual source files, butshould not be used in a global context, like header files.</p></li><li><p>use a <span class="emphasis"><em>fullyqualified name</em></span>for each library symbol(i.e. <code class="code">std::string</code>, <code class="code">std::cout</code>) Always can beused, and usually enhanced, by strategic use of typedefs. (In thecases where the qualified verbiage becomes unwieldy.)</p></li></ul></div></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.comp"></a>Using Namespace Composition</h3></div></div></div><p>Best practice in programming suggests sequestering new data orfunctionality in a sanely-named, unique namespace wheneverpossible. This is considered an advantage over dumping everything inthe global namespace, as then name look-up can be explicitly enabled ordisabled as above, symbols are consistently mangled without repetitivenaming prefixes or macros, etc.</p><p>For instance, consider a project that defines most of its classes in <code class="code">namespace gtk</code>. It is possible toadapt <code class="code">namespace gtk</code> to <code class="code">namespace std</code> by using a C++-feature called<span class="emphasis"><em>namespace composition</em></span>. This is what happens ifa <span class="emphasis"><em>using</em></span>-declaration is put into anamespace-definition: the imported symbol(s) gets imported into thecurrently active namespace(s). For example:</p><pre class="programlisting">namespace gtk{using std::string;using std::tr1::array;class Window { ... };}</pre><p>In this example, <code class="code">std::string</code> gets imported into<code class="code">namespace gtk</code>. The result is that use of<code class="code">std::string</code> inside namespace gtk can just use <code class="code">string</code>, without the explicit qualification.As an added bonus,<code class="code">std::string</code> does not get imported intothe global namespace. Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the<code class="code">using</code>-declarations can wrapped in macros thatare set based on autoconf-tests to either "" or i.e. <code class="code">usingstd::string;</code> (depending on whether the system haslibstdc++ in <code class="code">std::</code> or not). (ideas from<code class="email"><<a class="email" href="mailto:llewelly@dbritsch.dsl.xmission.com">llewelly@dbritsch.dsl.xmission.com</a>></code>, Karl Nelson <code class="email"><<a class="email" href="mailto:kenelson@ece.ucdavis.edu">kenelson@ece.ucdavis.edu</a>></code>)</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="using_headers.html">Prev</a>Β </td><td width="20%" align="center"><a accesskey="u" href="using.html">Up</a></td><td width="40%" align="right">Β <a accesskey="n" href="using_macros.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">HeadersΒ </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top">Β Macros</td></tr></table></div></body></html>