using namespace std

is it bad practice using this as global?

Page 1 of 1

4 Replies - 1526 Views - Last Post: 05 January 2011 - 03:06 PM Rate Topic: -----

#1 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

using namespace std

Posted 05 January 2011 - 02:10 PM

I see often coders using "using namespace std;" above or below the directives. This would technically be a global "variable"/statement...no? I have always heard there is not excuse nor reason to ever use global variables in C++. I admit, I use it every program because it seems faster and easier than 20,000 std:: cout and std:: endl; - So is it "bad" practice in a work environment (i.e. software development) to use this? Or is it just "it depends" type things? Thanks for your time and answers.

Is This A Good Question/Topic? 1
  • +

Replies To: using namespace std

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: using namespace std

Posted 05 January 2011 - 02:26 PM

it's not a global variable, but it is a statement but not a global statement. take this code

#include <iostream>

void foo() {
   using namespace std;
   cout<<" world\n";
}
int main() {
   std::cout<<"hello";
   foo();
}



this is dosn't open the std namespace into global scope. i only use it in global scope for little test applications because it's bad practice. it's almost as bad (maybe even worse) as declaring a global variable. it took me a while to change my habits because i was so used to it. a better way is to just use the specific variable/function/class you want to use.

using std::cout;
using std::getline;
using std::string;


This post has been edited by ishkabible: 05 January 2011 - 02:40 PM

Was This Post Helpful? 2
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 3,000
  • Joined: 30-May 10

Re: using namespace std

Posted 05 January 2011 - 02:31 PM

Well a sole programmer writing student assignments would rarely step outside of what the std:: namespace offers.

But at the other end of the scale, a large company with many teams of programmers would have an awful job making sure there were no name conflicts across the entire organisation. So it might choose to give each 'team' their own namespace to work in.
If two teams create a function called update(), then resolving between them is very easily achieved with team1::update() and team2::update()

Prior to ANSI/ISO, programs began with say
#include <iostream.h> // in a global namespace

In ANSI/ISO C++, this becomes
#include <iostream> // in the std:: namespace

Basically, all you had to do to update was remove the .h from all the includes, and add in a
using namespace std;
and you were done (possibly)


There is a useful middle ground as well.
#include <iostream>
#include <string>
using std::cin;

int main()
{
  std::string   s;
  std::getline(cin,s);
  std::cout << s << std::endl;
}


You can abbreviate the ones you use the most of, and then use std:: in front of everything else.
Was This Post Helpful? 3
  • +
  • -

#4 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: using namespace std

Posted 05 January 2011 - 02:34 PM

View Postishkabible, on 05 January 2011 - 01:26 PM, said:

it's not a global variable, but it is a statement but not a global statement. take this code

#include <iostream>

void foo() {
   using namespace std;
   cout<<" world\n";
}
int main() {
   std::cout<<"hello";
   foo();
}




this is dosn't open the std namespace into global scope. i only use it for little test applications because it's bad practice. it's almost as bad (maybe even worse) as declaring a global variable. it took me a while to change my habits because i was so used to it. a better way is to just use the specific variable/function/class you want to use.

using std::cout;
using std::getline;
using std::string;



Yeah global "variable" was a bad choice of wording. I was just thinking in a global statement frame of mind as in
#include <iostream>
using namespace std;

int main(){
   cout << "whatever" << endl; // instead of std::cout << "whatever" << std::endl;
}


thanks for the response : )
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: using namespace std

Posted 05 January 2011 - 03:06 PM

As long as you know that conflicts may exist in namespace naming conventions and are aware of preventing them, than opening up an entire namespace global isn't a problem :)

Heres a crude example of what I mean by conflicts
#include <iostream>

namespace A
{
	int num = 10;
}

namespace B
{
	int num = 20;
}

int main()
{
    //we just let all hell break loose
	using namespace A;
	using namespace B;
	
	//what num are we talking about? the one in A or the one in B?
	std::cout<<num<<std::endl;


	return 0;
}


The way of preventing name conflict is like so
#include <iostream>

namespace A
{
	int num = 10;
}

namespace B
{
	int num = 20;
}

int main()
{
	
	std::cout<<"FROM NAMESPACE A : "<<A::num<<std::endl;
	std::cout<<"FROM NAMESPACE B : "<<B::num<<std::endl;


	return 0;
}



This post has been edited by ImaSexy: 05 January 2011 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1