xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/doc/html/manual/using_namespaces.html (revision 36ac495d2b3ea2b9d96377b2143ebfedac224b92)
1*36ac495dSmrg<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2*36ac495dSmrg<!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 Vsnapshot" /><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_dual_abi.html" title="Dual ABI" /><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 width="20%" align="left"><a accesskey="p" href="using_dual_abi.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Using</th><td width="20%" align="right"> <a accesskey="n" href="using_dynamic_or_shared.html">Next</a></td></tr></table><hr /></div><div class="section"><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="section"><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.
3*36ac495dSmrg</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>std</p><p>The ISO C++ standards specify that "all library entities are defined
4*36ac495dSmrgwithin namespace std." This includes namespaces nested
5*36ac495dSmrgwithin namespace <code class="code">std</code>, such as namespace
6*36ac495dSmrg<code class="code">std::chrono</code>.
7*36ac495dSmrg</p></li><li class="listitem"><p>abi</p><p>Specified by the C++ ABI. This ABI specifies a number of type and
8*36ac495dSmrgfunction APIs supplemental to those required by the ISO C++ Standard,
9*36ac495dSmrgbut necessary for interoperability.
10*36ac495dSmrg</p></li><li class="listitem"><p>__gnu_</p><p>Indicating one of several GNU extensions. Choices
11*36ac495dSmrginclude <code class="code">__gnu_cxx</code>, <code class="code">__gnu_debug</code>, <code class="code">__gnu_parallel</code>,
12*36ac495dSmrgand <code class="code">__gnu_pbds</code>.
13*36ac495dSmrg</p></li></ul></div><p> The library uses a number of inline namespaces as implementation
14*36ac495dSmrgdetails that are not intended for users to refer to directly, these include
15*36ac495dSmrg<code class="code">std::__detail</code>, <code class="code">std::__cxx11</code> and <code class="code">std::_V2</code>.
16*36ac495dSmrg</p><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" target="_top">documentation</a>.
17*36ac495dSmrg</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.std"></a>namespace std</h3></div></div></div><p>
18*36ac495dSmrg      One standard requirement is that the library components are defined
19*36ac495dSmrg      in <code class="code">namespace std::</code>. Thus, in order to use these types or
20*36ac495dSmrg      functions, one must do one of two things:
21*36ac495dSmrg</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>put a kind of <span class="emphasis"><em>using-declaration</em></span> in your source
22*36ac495dSmrg(either <code class="code">using namespace std;</code> or i.e. <code class="code">using
23*36ac495dSmrgstd::string;</code>) This approach works well for individual source files, but
24*36ac495dSmrgshould not be used in a global context, like header files.
25*36ac495dSmrg	  </p></li><li class="listitem"><p>use a <span class="emphasis"><em>fully
26*36ac495dSmrgqualified name</em></span> for each library symbol
27*36ac495dSmrg(i.e. <code class="code">std::string</code>, <code class="code">std::cout</code>) Always can be
28*36ac495dSmrgused, and usually enhanced, by strategic use of typedefs. (In the
29*36ac495dSmrgcases where the qualified verbiage becomes unwieldy.)
30*36ac495dSmrg	  </p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="manual.intro.using.namespaces.comp"></a>Using Namespace Composition</h3></div></div></div><p>
31*36ac495dSmrgBest practice in programming suggests sequestering new data or
32*36ac495dSmrgfunctionality in a sanely-named, unique namespace whenever
33*36ac495dSmrgpossible. This is considered an advantage over dumping everything in
34*36ac495dSmrgthe global namespace, as then name look-up can be explicitly enabled or
35*36ac495dSmrgdisabled as above, symbols are consistently mangled without repetitive
36*36ac495dSmrgnaming prefixes or macros, etc.
37*36ac495dSmrg</p><p>For instance, consider a project that defines most of its classes in <code class="code">namespace gtk</code>. It is possible to
38*36ac495dSmrg	adapt <code class="code">namespace gtk</code> to <code class="code">namespace std</code> by using a C++-feature called
39*36ac495dSmrg	<span class="emphasis"><em>namespace composition</em></span>. This is what happens if
40*36ac495dSmrg	a <span class="emphasis"><em>using</em></span>-declaration is put into a
41*36ac495dSmrg	namespace-definition: the imported symbol(s) gets imported into the
42*36ac495dSmrg	currently active namespace(s). For example:
43*36ac495dSmrg</p><pre class="programlisting">
44*36ac495dSmrgnamespace gtk
45*36ac495dSmrg{
46*36ac495dSmrg  using std::string;
47*36ac495dSmrg  using std::tr1::array;
48*36ac495dSmrg
49*36ac495dSmrg  class Window { ... };
50*36ac495dSmrg}
51*36ac495dSmrg</pre><p>
52*36ac495dSmrg	In this example, <code class="code">std::string</code> gets imported into
53*36ac495dSmrg	<code class="code">namespace gtk</code>.  The result is that use of
54*36ac495dSmrg	<code class="code">std::string</code> inside namespace gtk can just use <code class="code">string</code>, without the explicit qualification.
55*36ac495dSmrg	As an added bonus,
56*36ac495dSmrg	<code class="code">std::string</code> does not get imported into
57*36ac495dSmrg	the global namespace.  Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
58*36ac495dSmrg	<code class="code">using</code>-declarations can wrapped in macros that
59*36ac495dSmrg	are set based on autoconf-tests to either "" or i.e. <code class="code">using
60*36ac495dSmrg	  std::string;</code> (depending on whether the system has
61*36ac495dSmrg	libstdc++ in <code class="code">std::</code> or not).  (ideas from
62*36ac495dSmrg	Llewelly and Karl Nelson)
63*36ac495dSmrg</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="using_dual_abi.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_dynamic_or_shared.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Dual ABI </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Linking</td></tr></table></div></body></html>