Topic: What's wrong with using namespace std;


Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/09/29
Raw View
"Al Stevens" <alstevens@midifitz.com> writes:
> The using namespace std; declaration is often deprecated because it "puts
> the contents of the standard library into the global namespace," which is
> said to be a bad thing, because it effectively subverts the intentions of
> the namespace.

It's bad to put it in a header file, not in a source file. Header files
get included in lots of places, so a using declaration can mess things
up. If you want to use it in a source file, that's perfectly OK.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1999/09/29
Raw View
Al Stevens wrote in message ...

>Inasmuch as the programmer controls where the using namespace declaration
>goes (following all #includes, for example) and over the remainder of the
>translation unit, and inasmuch as non-public interface library external
>identifiers have underscore prefixes, and inasmuch as users are not
>permitted identifiers with underscore prefixes, I am curious as to why this
>usage is so often disparaged and if there are other pitfalls in it that I
am
>missing.

1) You might accidently use a std:: name because you forgot it was there.

  #include <algorithm>    // I want to use std::sort later
  using namespace std;

  namespace filestuff
  {
    class filename{ public: filename(const char*); ... };
    void swap(filename a, filename b);    // uses rename() on files.
  }
  using namespace filestuff;

  void foo(const char* file1, const char* file2)
  {
    swap(file1, file2);
  }
  ...

In foo() I thought I was calling filestuff::swap, but I was actually calling
std::swap.

2) The next version of C++ (or a TC) might add some names to std::.  This
will only break programs that either
  2a) Have macros that conflict with the new names.
  2b) Have a "using namespace std;"

3) You might try to compile your code on a system where the vendor added (or
is adding)  new, unreserved, names to std.  I believe SGI STL does this.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Al Stevens" <alstevens@midifitz.com>
Date: 1999/09/27
Raw View
Clipped and modified from another thread:

> #include <cstdio>
> using namespace std;
>(and Stroustrup even mentions it in D&E), the effect ... is to bring
everything from <cstdio> into the global
>namespace, even though the programmer specifically picked <cstdio> so as
>not to do that.

This response doesn't address the question from the other thread, which is
why I started a new thread.

The using namespace std; declaration is often deprecated because it "puts
the contents of the standard library into the global namespace," which is
said to be a bad thing, because it effectively subverts the intentions of
the namespace. Yet it doesn't really do that. Those identifiers are
effectively in the global namespace only from the point of the using
declaration until the end of the translation unit and only at compile time.
The namespace-enclosed identifiers continue to enjoy their protection from
collision with other external identifiers during link.

Inasmuch as the programmer controls where the using namespace declaration
goes (following all #includes, for example) and over the remainder of the
translation unit, and inasmuch as non-public interface library external
identifiers have underscore prefixes, and inasmuch as users are not
permitted identifiers with underscore prefixes, I am curious as to why this
usage is so often disparaged and if there are other pitfalls in it that I am
missing.

Where's the harm in using the using namespace std; declaration to avoid
coding all those std:: qualifiers?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]