Skip to content

Commit

Permalink
Clarifying the usage of 'using namespace' directives and 'using' decl…
Browse files Browse the repository at this point in the history
…arationsin the NameSpace Section
  • Loading branch information
ArvindReddySheelam committed Jul 28, 2024
1 parent a164d25 commit 0a054fc
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions cppguide.html
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ <h3 id="Names_and_Order_of_Includes">Names and Order of Includes</h3>

#include &lt;string&gt;
#include &lt;vector&gt;

#include "base/basictypes.h"
#include "foo/server/bar.h"
#include "third_party/absl/flags/flag.h"
Expand Down Expand Up @@ -526,12 +525,34 @@ <h3 id="Namespaces">Namespaces</h3>

<p>With few exceptions, place code in a namespace. Namespaces
should have unique names based on the project name, and possibly
its path. Do not use <i>using-directives</i> (e.g.,
<code>using namespace foo</code>). Do not use
its path. Do not use <i>using-namespace</i>directives (e.g.,
<code>using namespace foo;</code>). Do not use
inline namespaces. For unnamed namespaces, see
<a href="#Internal_Linkage">Internal Linkage</a>.


</p><p class="definition"></p>
<h4>Using Declaration:</h4>
<p>Using declaration are allowed and can be used to refer to specific symbol from a namespace. For example</p>
<pre><code>Using ::foo::bar;</code></pre>

<h4>Additional Note With an Example:</h4>

<h5>Using NameSpace Directives: </h5>
<p>Using namespace directives brings the all the specified symbols/built-in-types from the specified NameSpace into the whatever current scope you are in, which leads to naming conflict and confusion(ambiguity). Therefore, they should be avoided.</p>

<p><strong>Example: </strong></p>
<pre><code>//Avoid This Practice:
using namespace std;
std::cout,std::endl or somethinglike std::string st = "Hello";</code></pre>

<h5>Using Declarations:</h5>
<p>Using Declarations allow you to bring namespace into current scope. This practice helps in avoiding the ambiguity(confusion) and which eventually keeps the code clear and manageable.</p>
<p><strong>Example to Use:</strong></p>
<pre><code>//Accepted
using namespace std;
cout,abs() or string str = "Hello,World";</code></pre>

<p>Namespaces subdivide the global scope
into distinct, named scopes, and so are useful for preventing
name collisions in the global scope.</p>
Expand Down

0 comments on commit 0a054fc

Please sign in to comment.