
{"id":1071,"date":"2014-11-30T13:06:03","date_gmt":"2014-11-30T13:06:03","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=1071"},"modified":"2014-11-30T13:56:55","modified_gmt":"2014-11-30T13:56:55","slug":"common-reasons-of-using-namespaces-in-c-projects","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=1071","title":{"rendered":"Common reasons of using  namespaces in C++ projects"},"content":{"rendered":"<p><span style=\"color: #333333;\">Namespaces were introduced to the C++ Standard in 1995\u00a0and usually they are defined like this:<\/span><\/p>\n<blockquote><p>A namespace defines a new scope. They provide a way to avoid name collisions.<\/p><\/blockquote>\n<p><span style=\"color: #252525;\">Namespaces in C++ are most often used to avoid\u00a0<\/span>naming collisions<span style=\"color: #252525;\">. Although namespaces are used extensively in recent C++ code, most older code does not use this facility.\u00a0<\/span><!--more--><\/p>\n<p>After exploring the source code of many C++ projects, here are some common reasons of using the namespaces in these projects.<\/p>\n<p><strong>1- Avoid\u00a0name collisions.<\/strong><\/p>\n<p>As speicified before it&#8217;s the common reason, in this case their use are\u00a0just useful for the compiler, no added value for the developer to make the code more readable and maintanable.<\/p>\n<p><strong>2- Modularize the application<\/strong><\/p>\n<p>Modern C++ librarires use extensively the namespaces to modalirize their code base, and they use the \u00a0&#8220;Namespace-by-feature&#8221; approach.\u00a0Namespace-by-feature uses namespaces\u00a0to reflect the feature set. It places all items related to a single feature (and only that feature) into a single namespace. This results in namespaces\u00a0with high cohesion and high modularity, and with minimal coupling between namespaces. Items that work closely together are placed next to each other.<\/p>\n<p>Boost is the best example of grouping by feature, it contains thousands of namespaces, each one is used to group a specific feature.<\/p>\n<p><strong>3- Anonymous namespace.<\/strong><\/p>\n<p><span style=\"color: #333333;\">Namespace with no name avoids making global static variable. The \u201canonymous\u201d namespace you have created will only be accessible within the file you created it in.<\/span><\/p>\n<p><strong>4- workarround of the enum issue.<\/strong><\/p>\n<p><span style=\"color: #111111;\">\u201cTraditional\u201d enums in C++ export their enumerators in the surrounding scope ,which can lead to name collisions, if two different enums in the same have scope define enumerators with the same name,<\/span><\/p>\n<p>I<span style=\"color: #333333;\">n a large project, you would not be guaranteed that two distinct enums don\u2019t both called with the same name. This issue was resolved in C++11, using\u00a0\u00a0<\/span><strong style=\"color: #333333;\">enum class<\/strong><span style=\"color: #333333;\">\u00a0which will implicitly scope the enum values within the enum\u2019s name.\u00a0<\/span><\/p>\n<p>Many years ago the trick of declaring an enum inside a namespace is used, for example instead of declaring an enum like this<\/p>\n<pre class=\"lang-cpp prettyprint prettyprinted\" style=\"color: #000000;\"><code><span class=\"kwd\" style=\"color: #00008b;\">enum<\/span><span class=\"pln\" style=\"color: #000000;\"> status<\/span><span class=\"pun\" style=\"color: #000000;\">{<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n  status_ok<\/span><span class=\"pun\" style=\"color: #000000;\">,<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n  status_error\r\n<\/span><span class=\"pun\" style=\"color: #000000;\">};<\/span><\/code><\/pre>\n<p>it&#8217;s declared inside an namespace:<\/p>\n<pre class=\"lang-cpp prettyprint prettyprinted\" style=\"color: #000000;\"><code><span class=\"kwd\" style=\"color: #00008b;\">namespace<\/span><span class=\"pln\" style=\"color: #000000;\"> status<\/span><span class=\"pun\" style=\"color: #000000;\">{\r\n<\/span><span class=\"kwd\" style=\"color: #00008b;\">       enum<\/span><span class=\"pln\" style=\"color: #000000;\"> status<\/span><span class=\"pun\" style=\"color: #000000;\">{<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n         ok<\/span><span class=\"pun\" style=\"color: #000000;\">,<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n          error\r\n  <\/span><span class=\"pun\" style=\"color: #000000;\">};\r\n<\/span><span class=\"pun\" style=\"color: #000000;\">}<\/span><\/code><\/pre>\n<p>Many C++ projects use this trick, for example the Unreal Engine source code use widely this technique.<\/p>\n<p><strong>5- Hiding details by convention<\/strong><\/p>\n<p>For templated libraries where the code is implemented in header files, it&#8217;s<span style=\"color: #555555;\">\u00a0interesting to find a way to inform the library user that he dont need to use directly some specific types because they concern only the implementation.\u00a0In\u00a0C# the \u201cinternal\u201d keyword did the job, but in C++ there\u2019s no way to hide public types to the library user.<\/span><\/p>\n<p>A common idiom in modern C++, pioneered by the developers of the Boost libraries, is to separate symbols that form part of the implementation of your module (that is, don\u2019t form part of the public API) but that have to be publicly available into a separate sub-namespace, by convention named detail.<\/p>\n<p>For example in the boost::math documentation, it&#8217;s specified that:<\/p>\n<blockquote><p><span style=\"color: #000000;\">Functions not intended for use by applications are in\u00a0<\/span><code class=\"computeroutput\" style=\"color: #000000;\"><span class=\"identifier\" style=\"color: #000000;\">boost<\/span><span class=\"special\" style=\"color: #707070;\">::<\/span><span class=\"identifier\" style=\"color: #000000;\">math<\/span><span class=\"special\" style=\"color: #707070;\">::<\/span><span class=\"identifier\" style=\"color: #000000;\">detail<\/span><\/code><span style=\"color: #000000;\">.<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Namespaces were introduced to the C++ Standard in 1995\u00a0and usually they are defined like this: A namespace defines a new scope. They provide a way to avoid name collisions. Namespaces in C++ are most often used to avoid\u00a0naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=1071\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Common reasons of using  namespaces in C++ projects&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1071","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1071"}],"version-history":[{"count":14,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1071\/revisions"}],"predecessor-version":[{"id":1086,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1071\/revisions\/1086"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}