{"id":79,"date":"2017-06-04T13:16:18","date_gmt":"2017-06-04T13:16:18","guid":{"rendered":"http:\/\/cppdepend.com\/wordpress\/?p=79"},"modified":"2017-06-26T22:47:57","modified_gmt":"2017-06-26T22:47:57","slug":"5-common-reasons-of-using-namespaces-in-c-projects","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/5-common-reasons-of-using-namespaces-in-c-projects\/","title":{"rendered":"5 Common reasons of using namespaces in C++ projects"},"content":{"rendered":"<p>Namespaces were introduced to the C++ Standard in 1995\u00a0and usually they are defined like this:<\/p>\n<blockquote><p>A namespace defines a new scope. They provide a way to avoid name collisions.<\/p><\/blockquote>\n<p>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 facility.\u00a0<span id=\"more-1071\"><\/span><\/p>\n<p>After exploring the source code of many C++ projects, here are some common reasons of using the namespaces in these projects.<!--more--><\/p>\n<p><strong>1- Avoid\u00a0name collisions.<\/strong><\/p>\n<p>As speicified before it\u2019s 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\u201cNamespace-by-feature\u201d 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>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.<\/p>\n<p><strong>4- workarround of the enum issue.<\/strong><\/p>\n<p>\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,<\/p>\n<p>In 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<strong>enum class<\/strong>\u00a0which will implicitly scope the enum values within the enum\u2019s name.<\/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\"><code><span class=\"kwd\">enum<\/span><span class=\"pln\"> status<\/span><span class=\"pun\">{<\/span><span class=\"pln\">\r\n  status_ok<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\r\n  status_error\r\n<\/span><span class=\"pun\">};<\/span><\/code><\/pre>\n<p>it\u2019s declared inside an namespace:<\/p>\n<pre class=\"lang-cpp prettyprint prettyprinted\"><code><span class=\"kwd\">namespace<\/span><span class=\"pln\"> status<\/span><span class=\"pun\">{\r\n<\/span><span class=\"kwd\">       enum<\/span><span class=\"pln\"> status<\/span><span class=\"pun\">{<\/span><span class=\"pln\">\r\n         ok<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\r\n          error\r\n  <\/span><span class=\"pun\">};\r\n<\/span><span class=\"pun\">}<\/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\u2019s\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.<\/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\u2019s specified that:<\/p>\n<blockquote><p>Functions not intended for use by applications are in\u00a0<code class=\"computeroutput\"><span class=\"identifier\">boost<\/span><span class=\"special\">::<\/span><span class=\"identifier\">math<\/span><span class=\"special\">::<\/span><span class=\"identifier\">detail<\/span><\/code>.<\/p><\/blockquote>\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:\/\/cppdepend.com\/blog\/5-common-reasons-of-using-namespaces-in-c-projects\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;5 Common reasons of using namespaces in C++ projects&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cplusplus"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":1,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}