what does using namespace std mean?

what does using namespace std mean?

Post by Peter van » Fri, 22 Aug 2003 21:36:19



Namespaces are used to avoid name clashes with other libraries. C++
namespace are _somewhat_ similar to Java packages. Standard library
class and functions reside in the std namespace. Without the 'using
namespace' directive you would have to explicitly add std:: before every
identier of the standard library:

int main() {
std::string name;
std::cout << "What is your name?";
std::cin >> name;
std::cout << "Welcome, " << name << ".";
return 0;
}

The 'using namespace std;' directive pulls the identifiers in the std
namespace into the current namespace so that you don't have to
explicitly specify that you are refering to a identifier from the std
namespace. The 'using namespace' directive should be used judiciously in
the smallest posible scope and never in header files. To liberal use of
'using namespace' directives may lead to unexpected results.

BTW. <iostream.h> is non-standard header it should be <iostream>. If the
tutorial can't get these simple things right one might wonder about the
quality and accuracy of rest of the tutorial
 
 
 

what does using namespace std mean?

Post by Sean Frale » Fri, 22 Aug 2003 21:50:11


When C++ was standardized by ANSI/ISO, some new features were introduced,
namespaces being one of them. They are basically a way to prevent
conflicts between items from different libraries having the same name. For
a detailed explanation, look up a couple of web sites.

With the introduction of namespaces into the standard, some changes were
also made to the preferred methods of including headers from the standard
library. The main issues are as follows:

1) When including items from the standard library, don't use the ".h" file
suffix anymore. Just use the name of the file without the suffix.

example: #include <iostream.h>
becomes
#include <iostream>

2) When including file that were part of the Standard C Library, put a 'c'
at the beginning of the name and drop the ".h" suffix.

example: #include <stdlib.h>
becomes
#include <cstdlib>

3) The old method of including standard library headers is still perfectly
functional, since the current standard has the old ones around for backwards
compatibility. If the standard changes, and allows for the old ones to be
removed, then code that uses the old method will fail to compile on a
standard compliant compiler.

4) This only applies to standard library files. Includes from your own
project, and those from other libraries still use the old method.


As for the "using namespace std" line:

All of the contents of the standard library are part of the namespace "std".
To use the components of the library, you need to tell the compiler so.
That is what the "using" keyword is for. "using namespace std" is telling
the compiler that you are using everything in the std namespace. Keep in
mind that if you use the new method of including standard library files,
compiling will fail if you don't do this. There is a bit more to the use
of the "using" keyword than this, and can be found in other sources.

 
 
 

what does using namespace std mean?

Post by Kevin Good » Sat, 23 Aug 2003 04:58:54


This is incorrect. <iostream.h>, for example is *not* part of the C++
Standard. The only headers that are part of the standard and end with .h
are those that were inherited from C, and they are deprecated in favor
of the headers that add a 'c' on the front and drop the '.h'.

Sean, why did you set the "followup-to" field to some random thing? That
makes replying kind of difficult.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
 
 

what does using namespace std mean?

Post by daveoh7 » Sat, 23 Aug 2003 19:36:14


"using" in C++ is pretty much the same thing as "import" in Java. The
analogy does not go all the way; C++ namespaces are more malleable
than Java packages. But just the "using" keyword itself is very
similar to Java's "import". Just type "using" instead of "import", and
"::" instead of ".", and it's the same. All the decisions about
importing the whole thing vs. importing one name at a time vs. not
important and having to spell everything out are identical.

There are two differences, even there. One is, C++ only has a std.
There are no std::stl, std::iostreams, etc., just std. If you pull in
all the names in std, you will get a lot more than if you pulled in a
whole package in Java. Also, Java imports some names (java.lang.*)
whether you ask it to or not. C++ does not do this. If you want names
out of std, you have to say so.

Past that, differences accumulate, and I'd check the FAQ. C++
namespaces are only for names. Everything related to header files,
compilation, linking against libraries (if you do that), etc., is the
same as it always was.

--
Dave O'Hearn
 
 
 

what does using namespace std mean?

Post by Agent Muld » Sat, 23 Aug 2003 19:52:45

Sony> i noticed that the author used the line using namespace std.
Sony> what is that?

D O'H> "using" in C++ is pretty much the same
D OH> thing as "import" in Java.


Study this newby example:

class *** {};//comment this out and it compiles 1
namespace Green
{
class *** {};//OR comment this out and it compiles 2
}
using namespace Green;//OR comment this out and it compiles 3
int main(int argc,char**argv)
{
*** *** ;//OR comment this out and it compiles 4
return 0;
}

1. You removed class *** from the global namespace.
Since you use namespace Green, Green:: *** will be
used

2. You removed class *** from namespace Green.
The global class *** is used. Global class ***
is also known as :: *** .

3. You no longer use namespace Green, so :: *** is used

4. The ambigity between two version of class *** still
exists in the namegiving, but it is never used so nobody
cares. You can also change this line to one of these:

:: *** *** ; //OK. Global *** used
Green:: *** *** ; //OK. Green:: *** used
 
 
 

what does using namespace std mean?

Post by daveoh7 » Sun, 24 Aug 2003 09:45:33


I can't tell what the point of the example is. I also can't tell if
your response is directed to me, or whether you only replied to me
because I was the most-recent poster in the thread at the time.

You seem to be pointing out some idiosynchratic behaviors of C++
namespaces. I am sure it is full of them, but I use namespaces myself,
and I have never run into idiosynchratic behavior. If used simply,
they are very simple things.

--
Dave O'Hearn
 
 
 

what does using namespace std mean?

Post by Agent Muld » Sun, 24 Aug 2003 20:06:03

D O'H> I can't tell what the point of the example is. I also can't tell if
D O'H> your response is directed to me, or whether you only replied to me
D O'H> because I was the most-recent poster in the thread at the time.

The point of the example is to give the reader
the AHA-erlebnis. Is that all there is to namespace?
That's what I try to invoke. I responded to your
mail but I included the original question to make
it 'stand-alone'.

D O'H> You seem to be pointing out some idiosynchratic behaviors of C++
D O'H> namespaces. I am sure it is full of them, but I use namespaces
myself,
D O'H> and I have never run into idiosynchratic behavior. If used simply,
D O'H> they are very simple things.

I don't know how you learn, but I always look for
idiosynchratic syntax, weird structures, malicious
expansions (if not pathological), because only from
that perspective can I appreciate the simplicity of
the language.

-X