tags:

views:

207

answers:

3

Is using namespace std; a standard C++ function?

+15  A: 

No. It's what's called a using directive. I's outlined in §7.3.4 in the standard:

A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (3.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace.

Essentially, it takes everything in the namespace you specify and moves in into the namespace the directive was used.

GMan
Boy you are quick! Do you have the standard open all the time? :)
Nikolai N Fetissov
I think they missed a real opportunity here. I always thought they should have had the using directive essentially "fabricate" a scope surrounding the current one, and put the names there. This way, those names would be visible and would be found by a lookup in preference to any from surrounding scopes, but would *not* conflict with identical names in the current scope. This would eliminate (most of?) their current drawbacks without losing their convenience.
Jerry Coffin
@Nikolai: :] I'm practicing my draw...One day I will stand before litb in the dusty roads in the town of C++. The road is empty and dry, but curious eyes peer through doors and windows in wonderment as the conflict unfolds. I look at him right in the eyes, and he looks right on back. Then, when the town clock begins to chime, faster than Bjarne himself we will draw out our standard. Flipping through the pages, searching for the quote, the dust hasn't settled when I stand victorious, litb fallen to the ground in defeat; for I have mastered The Standard.
GMan
@GMan: Maybe -- but Johannes (litb) has been hanging out on comp.std.c++ lately...
Jerry Coffin
@Jerry: I know :( I forgot to mention I poisoned him previous to our encounter.
GMan
Wow, you guys! In most posts, like both your's and litb's answers, however the wild wild west theateric narration was fun to imagine :D
legends2k
I guess litb will see these and have a good laugh :}
legends2k
You guys need to get a life! Oh, me too probably :)
Nikolai N Fetissov
@GMan: that might help. Then again, I've written 288 posts there vs. his 75, and I'd be the last to claim that I've mastered the standard nearly 4 times as well. The C90 standard's easier -- at one time, I could quote section numbers from it from memory, but I'm recovering now...
Jerry Coffin
@Jerry: Good point with fabricating enclosing scopes. Too late now though :|
Georg Fritzsche
@gf:I'm not sure it really is too late -- I don't think it would change the behavior of any program that's currently conforming. It would just give defined behavior to a whole set of programs that currently (at least potentially) aren't.
Jerry Coffin
@Jerry: Hm, wouldn't that break some corner-cases like SFINAE-patterns that test for ambiguities?
Georg Fritzsche
@gf:I suppose it's possible -- SFINAE gets used in enough strange ways that about the only way you could even get a decent idea would be to implement it and try to compile some things like Boost, and see how much broke, then try to figure out how much of that was because of what you did intentionally, and how much due to some other bug you introduced (and on a project this size, at least a few of those would be nearly inevitable...)
Jerry Coffin
+3  A: 

It is not a function.

In the following

 using namespace std; 

the keyword using is used as a directive to introduce an entire namespace (std in this case).

Note: Always prefer using declaration over using directive

Prasoon Saurav
A: 

No function is part of the C++ language; it's something you write or something which is already written as library and made available to you (like Standard C++ Library). Such libraries have functions and variables put up inside segregations called namespaces. To call a function within a namespace, you follow the syntax namespace::function_name() syntax.

E.g. std::cout << "Hi!";

While you always call a function with the function's name followed by parenthesis within with you pass arguments to the function, if any. Like function_name(args);. So to what you've asked: No, it isn't a function.

using namespace std is a directive (you can think of it as a declaration to the compiler saying whatever is inside the std namespace can be used without any qualifiers from here on till the end of this compilation unit (.cpp + headers associated within) i.e.

std::cout << "Hello!"; can be used as cout << "Hello!"; from there on.

legends2k