xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/doc/html/manual/using_namespaces.html (revision d79abf08584d17438738b62a4ac1b023f122bf52)
14fee23f9Smrg<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2*d79abf08Smrg<!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.
348fb7bfaSmrg</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
44fee23f9Smrgwithin namespace std." This includes namespaces nested
5f30ff588Smrgwithin namespace <code class="code">std</code>, such as namespace
6f30ff588Smrg<code class="code">std::chrono</code>.
74fee23f9Smrg</p></li><li class="listitem"><p>abi</p><p>Specified by the C++ ABI. This ABI specifies a number of type and
84fee23f9Smrgfunction APIs supplemental to those required by the ISO C++ Standard,
94fee23f9Smrgbut necessary for interoperability.
104fee23f9Smrg</p></li><li class="listitem"><p>__gnu_</p><p>Indicating one of several GNU extensions. Choices
114fee23f9Smrginclude <code class="code">__gnu_cxx</code>, <code class="code">__gnu_debug</code>, <code class="code">__gnu_parallel</code>,
124fee23f9Smrgand <code class="code">__gnu_pbds</code>.
134d5abbe8Smrg</p></li></ul></div><p> The library uses a number of inline namespaces as implementation
144d5abbe8Smrgdetails that are not intended for users to refer to directly, these include
154d5abbe8Smrg<code class="code">std::__detail</code>, <code class="code">std::__cxx11</code> and <code class="code">std::_V2</code>.
164d5abbe8Smrg</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>.
1748fb7bfaSmrg</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>
184fee23f9Smrg      One standard requirement is that the library components are defined
194fee23f9Smrg      in <code class="code">namespace std::</code>. Thus, in order to use these types or
204fee23f9Smrg      functions, one must do one of two things:
2148fb7bfaSmrg</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
224fee23f9Smrg(either <code class="code">using namespace std;</code> or i.e. <code class="code">using
234fee23f9Smrgstd::string;</code>) This approach works well for individual source files, but
244fee23f9Smrgshould not be used in a global context, like header files.
254fee23f9Smrg	  </p></li><li class="listitem"><p>use a <span class="emphasis"><em>fully
264fee23f9Smrgqualified name</em></span> for each library symbol
274fee23f9Smrg(i.e. <code class="code">std::string</code>, <code class="code">std::cout</code>) Always can be
284fee23f9Smrgused, and usually enhanced, by strategic use of typedefs. (In the
294fee23f9Smrgcases where the qualified verbiage becomes unwieldy.)
3048fb7bfaSmrg	  </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>
314fee23f9SmrgBest practice in programming suggests sequestering new data or
324fee23f9Smrgfunctionality in a sanely-named, unique namespace whenever
334fee23f9Smrgpossible. This is considered an advantage over dumping everything in
344fee23f9Smrgthe global namespace, as then name look-up can be explicitly enabled or
354fee23f9Smrgdisabled as above, symbols are consistently mangled without repetitive
364fee23f9Smrgnaming prefixes or macros, etc.
374fee23f9Smrg</p><p>For instance, consider a project that defines most of its classes in <code class="code">namespace gtk</code>. It is possible to
384fee23f9Smrg	adapt <code class="code">namespace gtk</code> to <code class="code">namespace std</code> by using a C++-feature called
394fee23f9Smrg	<span class="emphasis"><em>namespace composition</em></span>. This is what happens if
404fee23f9Smrg	a <span class="emphasis"><em>using</em></span>-declaration is put into a
414fee23f9Smrg	namespace-definition: the imported symbol(s) gets imported into the
424fee23f9Smrg	currently active namespace(s). For example:
434fee23f9Smrg</p><pre class="programlisting">
444fee23f9Smrgnamespace gtk
454fee23f9Smrg{
464fee23f9Smrg  using std::string;
474fee23f9Smrg  using std::tr1::array;
484fee23f9Smrg
494fee23f9Smrg  class Window { ... };
504fee23f9Smrg}
514fee23f9Smrg</pre><p>
524fee23f9Smrg	In this example, <code class="code">std::string</code> gets imported into
534fee23f9Smrg	<code class="code">namespace gtk</code>.  The result is that use of
544fee23f9Smrg	<code class="code">std::string</code> inside namespace gtk can just use <code class="code">string</code>, without the explicit qualification.
554fee23f9Smrg	As an added bonus,
564fee23f9Smrg	<code class="code">std::string</code> does not get imported into
574fee23f9Smrg	the global namespace.  Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
584fee23f9Smrg	<code class="code">using</code>-declarations can wrapped in macros that
594fee23f9Smrg	are set based on autoconf-tests to either "" or i.e. <code class="code">using
604fee23f9Smrg	  std::string;</code> (depending on whether the system has
614fee23f9Smrg	libstdc++ in <code class="code">std::</code> or not).  (ideas from
624fee23f9Smrg	Llewelly and Karl Nelson)
634d5abbe8Smrg</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>