6 Replies - 2949 Views - Last Post: 19 April 2012 - 01:12 PM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Alternative to using namespace std

Posted 19 April 2012 - 08:00 AM

Hi everybody,
I'm trying to create a header of string constants for easy reference, and it currently looks like this:

#ifndef DRAWCONSTANTS_H
#define DRAWCONSTANTS_H

using namespace std;
#include <string>

// Standardized color strings.
const string BLACK = "black";
const string WHITE = "white";
const string GRAY = "gray";
const string RED = "red";
const string PINK = "pink";
const string ORANGE = "orange";
const string YELLOW = "yellow";
const string LIGHT_GREEN = "light_green";
const string GREEN = "green";
const string LIGHT_BLUE = "light_blue";
const string BLUE = "blue";
const string PURPLE = "purple";

#endif /* DRAWINGCONSTANTS_H */



It works fine, no major problems here. But, I've heard a lot that using the declaration "using namespace std" is usually a bad thing. My question is, how much does that really matter, and if it does matter, is prefixing string declarations with "std::" absolutely the only way to make things work?

It seems like, in general, it's a matter of preference, but if there's a significant reason not to do things this way, please bring it to my attention.

EDIT: Just to be clear, I don't mean this as a general "how do you use the standard namespace" question, that I understand. My question is specifically, are there any special considerations for it that I might not know about when it's being used solely in a file of defined constants?

Thanks,
Zel2008

This post has been edited by Zel2008: 19 April 2012 - 08:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Alternative to using namespace std

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Alternative to using namespace std

Posted 19 April 2012 - 08:30 AM

The danger in name collisions. Some name in the std library might fight a local name in a program. If it's in the header, you're screwed.

Simple fix:
// using namespace std;
using std::string;



Now you're just pulling in the string alias.


If you don't even want that, just be explicit:
#include <string>

const std::string BLACK = "black";
const std::string WHITE = "white";


Was This Post Helpful? 1
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Alternative to using namespace std

Posted 19 April 2012 - 08:40 AM

Thanks baavgai,
I didn't realize you could use a "using" directive for something that specific; I will definitely start doing that instead.
Thanks again,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Alternative to using namespace std

Posted 19 April 2012 - 08:44 AM

The problem with using the "using namespace std;" clause it that you bring the entire std namespace into the global space. There are functions in the std namespace that have the same name as a function in the global namespace. Another option is the "using std::cout;" type statements. This will only bring in that particular member into the global namespace. But this can still lead to name clashes, much less often, but it is still possible. By scoping every function, class with the scope resolution operator:: (std::cout) you will avoid these name clashes.

In my opinion the "using namespace std;" method is usually adequate for small self-contained programs but as the program grows larger you will have more problems. Also I believe that it's use defeats the purpose of namespaces and should generally be avoided.

The second method "using std::string" also should be used sparingly in my opinion.

I myself prefer to scope each standard function using the scope operator::. If I do use either of the above methods I try to keep their use a local as possible. Remember you can use these statements inside a function or a single of block of code. I normally will never use anything but the scope operator:: inside an include file, or any file that will be included in another file (template inclusions). By keeping the using declarations as local as possible you will have fewer problems.

#include <iostream>
#include <string>

void mFunct();

int main()
{
   using namespace std; // Only valid in this function:
   cout << "HELLO WORLD";
   mFunct();
   return(0);
}

void mFunct()
{
   { 
      using std::string;
      string hello = "HELLO WORLD";
      //cout << hello << endl;     // error cout, endl not in scope.
      std::cout << hello << std::endl;
    }
    { 
       //string help = "HELP"; // error string not in scope.
       std::string help = "HELP";
       using std::cout;
       using std::endl;
       cout << help << endl;
   }
}



Jim

This post has been edited by jimblumberg: 19 April 2012 - 08:44 AM

Was This Post Helpful? 1
  • +
  • -

#5 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Alternative to using namespace std

Posted 19 April 2012 - 12:29 PM

Thanks Jim,
That's another approach I didn't know about; I'll definitely keep it under consideration.
Thanks!
Zel2008
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Alternative to using namespace std

Posted 19 April 2012 - 01:07 PM

One more. The scope is top down, like any C++ declaration. So this works:
#include <iostream>

void log(const std::string &, std::ostream &out = std::cout);

int main() {
	log("Hello Word");
	return 0;
}

using namespace std;

void log(const string &s, ostream &out) {
	out << s << endl;
}



While I agree with limiting scope, if it's the only namespace going on I find this the most practical. It lets me honestly declare the prototypes that might ultimately find their why unto a header without killing myself.
Was This Post Helpful? 1
  • +
  • -

#7 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Alternative to using namespace std

Posted 19 April 2012 - 01:12 PM

Thanks again baavgai! :)
Wow, I had no idea I would learn so much on this thread! Absolutely keeping it for reference. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1