{"id":4148,"date":"2017-03-04T12:19:31","date_gmt":"2017-03-04T17:19:31","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=4148"},"modified":"2021-12-08T12:19:02","modified_gmt":"2021-12-08T17:19:02","slug":"using-namespace-std","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/using-namespace-std\/","title":{"rendered":"Why “using namespace std” is Considered Bad Practice"},"content":{"rendered":"\n

Every C++ programmer whether new or old is familiar with namespaces and more so “using namespace std”. In most C++ tutorials and courses this is taught early and with good reason. It is used to reduce the typing the C++ <\/a>programmer needs to do and also make the code more readable.<\/p>\n\n\n\n

using namespace… allows the programmer to reduce repeated typing of the said namespace. Take for instance the using namespace std<\/code>. When printing output to standard output you can use something like std::cout<\/code> every time you need to print out some information to the standard output for example.<\/p>\n\n\n\n

std::cout << "This is some text\\n";<\/code><\/pre>\n\n\n\n

With using namespace std it allows you to do the following:<\/p>\n\n\n\n

using namespace std\n ...\ncout << "This is some text\\n";<\/code><\/pre>\n\n\n\n

It’s that simple. At least things should be that simple but it is not always the case.<\/p>\n\n\n\n

So Whats Wrong With “using namespace std”<\/h2>\n\n\n\n

Well, this has nothing to do with performance but more of for lack of a better term… confusion. Lets us consider this.<\/p>\n\n\n\n

using namespace foo;\nusing namespace bar;<\/code><\/pre>\n\n\n\n

So from the above, we have assumed you are using two libraries called foo<\/em> and bar<\/em> respectively. We can assume there is a function called booboo()<\/code> in the foo<\/em> library and a couple of others too. In the bar<\/em> library there is a function called baabaa(<\/code>) and a couple of other unique functions too.<\/p>\n\n\n\n

Calling either of these functions works just fine in this context. Now let’s complicate things a little. You decide to upgrade to the latest version of foo<\/em> with a barrage of new features and bug fixes. Unfortunately, the foo<\/em> library happens to include a new function called baabaa()<\/code>.<\/p>\n\n\n\n

You have just introduced a conflict. The new version of foo<\/em> and the existing bar<\/em>, both import baabaa() into the global namespace. As far as C++ is concerned, this is not acceptable and the compiler will surely complain about this. You can imagine the headaches you have to potentially deal with especially if you are working with a large codebase. The nightmare in fixing this issue will probably drive you up the wall depending on the size of the codebase.<\/p>\n\n\n\n

This could have been avoided if the original functions were called foo::booboo()<\/code> and bar:baabaa()<\/code>. No conflict there. You now have the option of enjoying the benefits of foo::baabaa()<\/code> too without the risk of complicating your code.<\/p>\n\n\n\n

Be sure to avoid “using namespace<\/strong>” as much as possible. If you do use it and are aware of existing conflicts you can explicitly call the foo::baabaa()<\/code> and bar::baabaa()<\/code> where appropriate.<\/p>\n\n\n\n

Another way to gracefully handle this is to use specific tokens such as this:<\/p>\n\n\n\n

using std::cout<\/code> rather than using namespace std<\/code> if you are going to use a lot of cout<\/code> and know it will not cause conflicts.<\/p>\n","protected":false},"excerpt":{"rendered":"

Every C++ programmer whether new or old is familiar with namespaces and more so “using namespace std”. In most C++ tutorials and courses this is taught early and with good…<\/p>\n","protected":false},"author":1,"featured_media":4149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,27,16,17],"tags":[111,143,184,350,433,449,471,472,543,544,563],"yoast_head":"\nWhy is "using namespace std" Considered Bad Practice?<\/title>\n<meta name=\"description\" content=\"Here's why "using namespace std" is considered bad practice in that is can cause conflicts in your seemingly perfect C++ code\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/using-namespace-std\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Why is "using namespace std" Considered Bad Practice?\" \/>\n<meta property=\"og:description\" content=\"Here's why "using namespace std" is considered bad practice in that is can cause conflicts in your seemingly perfect C++ code\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/using-namespace-std\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-04T17:19:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-08T17:19:02+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"Why “using namespace std” is Considered Bad Practice\",\"datePublished\":\"2017-03-04T17:19:31+00:00\",\"dateModified\":\"2021-12-08T17:19:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/\"},\"wordCount\":434,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg\",\"keywords\":[\"C++\",\"Compilers\",\"Debugging\",\"Libraries\",\"Optimization\",\"Performance\",\"Programming\",\"Programming Languages\",\"Software Design\",\"Software development\",\"STL\"],\"articleSection\":[\"Articles\",\"Programming\",\"Technology\",\"Tips\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/local.brightwhiz\/using-namespace-std\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/\",\"url\":\"http:\/\/local.brightwhiz\/using-namespace-std\/\",\"name\":\"Why is \\\"using namespace std\\\" Considered Bad Practice?\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg\",\"datePublished\":\"2017-03-04T17:19:31+00:00\",\"dateModified\":\"2021-12-08T17:19:02+00:00\",\"description\":\"Here's why \\\"using namespace std\\\" is considered bad practice in that is can cause conflicts in your seemingly perfect C++ code\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/using-namespace-std\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg\",\"width\":1200,\"height\":630,\"caption\":\"using namespace std\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/using-namespace-std\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Why “using namespace std” is Considered Bad Practice\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/twitter.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Why is \"using namespace std\" Considered Bad Practice?","description":"Here's why \"using namespace std\" is considered bad practice in that is can cause conflicts in your seemingly perfect C++ code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/using-namespace-std\/","og_locale":"en_US","og_type":"article","og_title":"Why is \"using namespace std\" Considered Bad Practice?","og_description":"Here's why \"using namespace std\" is considered bad practice in that is can cause conflicts in your seemingly perfect C++ code","og_url":"http:\/\/local.brightwhiz\/using-namespace-std\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2017-03-04T17:19:31+00:00","article_modified_time":"2021-12-08T17:19:02+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/using-namespace-std\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/using-namespace-std\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"Why “using namespace std” is Considered Bad Practice","datePublished":"2017-03-04T17:19:31+00:00","dateModified":"2021-12-08T17:19:02+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/using-namespace-std\/"},"wordCount":434,"commentCount":0,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg","keywords":["C++","Compilers","Debugging","Libraries","Optimization","Performance","Programming","Programming Languages","Software Design","Software development","STL"],"articleSection":["Articles","Programming","Technology","Tips"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/local.brightwhiz\/using-namespace-std\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/using-namespace-std\/","url":"http:\/\/local.brightwhiz\/using-namespace-std\/","name":"Why is \"using namespace std\" Considered Bad Practice?","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg","datePublished":"2017-03-04T17:19:31+00:00","dateModified":"2021-12-08T17:19:02+00:00","description":"Here's why \"using namespace std\" is considered bad practice in that is can cause conflicts in your seemingly perfect C++ code","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/using-namespace-std\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/using-namespace-std\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/using-namespace-std\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2017\/03\/using-namespace-std.jpg","width":1200,"height":630,"caption":"using namespace std"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/using-namespace-std\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"Why “using namespace std” is Considered Bad Practice"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/twitter.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/4148"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=4148"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/4148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/4149"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=4148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=4148"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=4148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}