declared or defined???

It's been quite a while I'm struggling to resolve the exact usage of "using namespace std". I doubt if all classes are declared in that special namespace 'std',but not defined,right?? Because when we want to use,let's say vector or stack,we have to include its' respective header file,I guess that's where its' meaning has been defined,aka defined what it means to be stack or vector. Kindly help me to resolve my doubt and I'd be thankful to anyone who will help me
To use a facility from the standard library, #include the appropriate header.
To use std::string, #include <string>


To be able to use unqualified names for all visible names in the namespace std, provide a using directive.
1
2
3
4
#include <string>

using namespace std ; // using directive
string str = "abcd" ; // unqualified name string is resolved to std::string 


First, namespaces are something different. See http://en.cppreference.com/w/cpp/language/namespace


Second, the using directive was a short-term workaround for porting MLOC projects from pre-namespaces C++ into namespace-supporting C++ Standard Library implementations (around year 2000). It should not be used in modern production code.

See http://www.gotw.ca/gotw/053.htm



[EDIT] Please, do not doublepost. Other thread: http://www.cplusplus.com/forum/beginner/222702/
Last edited on
> Second, the using directive was a short-term workaround for ...

It is still a very relevant feature of the language.

See: 14.4.3 Namespace Composition and 14.4.4 Composition and Selection
in 'The C++ Programming Language Fourth Edition' - Bjarne Stroustrup
Often, we want to compose an interface out of existing interfaces.
...
Combining composition (by using -directives) with selection (by using -declarations) yields the flexibility needed for most real-world examples. With these mechanisms, we can provide access to a variety of facilities in such a way that we resolve name clashes and ambiguities arising from their composition.



> It should not be used in modern production code.

As a hard and fast rule, this particular homily applies only to header files, and to those parts of implementation files which are before a #include directive.

In short: You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable.
...
But using declarations and directives are for your coding convenience, and you shouldn't use them in a way that affects someone else's code. In particular, don't write them anywhere they could be followed by someone else's code: Specifically, don't write them in header files (which are meant to be included in an unbounded number of implementation files, and you shouldn't mess with the meaning of that other code) or before an #include (you really don't want to mess with the meaning of code in someone else's header).

- Sutter and Alexandrescu in 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices'


SF.6: Use using namespace directives ... for foundation libraries (such as std) ...

using namespace can lead to name clashes, so it should be used sparingly. However, it is not always possible to qualify every name from a namespace in user code (e.g., during transition) and sometimes a namespace is so fundamental and prevalent in a code base, that consistent qualification would be verbose and distracting.

CoreGuideLines https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rs-using
Topic archived. No new replies allowed.