Re: Elegant way to do this?

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/11/04


Date: Sat, 11 Dec 2004 12:23:56 GMT


"Rich" <Someone@somewhere.com> wrote in message
news:cpeknq$41s$1@titan.btinternet.com...
>
<SNIP>
>
> One quick question
> I notice from a lot of code examples posted here that using
> namespace std is rarely if ever used and instead cout or
> cin or wahtever is preceeded by a namespace resolution
> operator ::
>
> This seems like more work than using the using statement.
>
> I would imagine that there is a very good reason for the std:: use
> rather than a using statement, or is it just a matter of preference?
>

To answer your question about why this is done:

     using std::cout;
     using std::cin;
     ...

rather than this:

     using namespace std;

In a nutshell, to reduce the possibility of name clashes occuring by
exposing only the names that actually *need* to be exposed in order for the
code to compile. It is for this very purpose that the namespace facility was
introduced. To then expose all names within the namespace, which is what
this:

     using namespace std;

does, pretty well makes the namespace facility's presence redundant.

A simple name clash example. I am creating an enumeration:

    enum Months { jan, feb, ..., oct, nov, dec };

and have exposed the entire 'std' namespace. Will my code compile ? No,
because two names used in the enumeration, namely:

    oct

and:

    dec

also occur in the 'std' namespace. The compilation error was due to a
totally unnecessary [not to mention bothersome and time wasting to find and
fix] name clash, something which would not have occured had I only exposed
only the names actually needed.

I daresay this practice is seen in a lot of introductory text book code
probably to eliminate distracting elements thus keep the code compact and
help the reader remain focussed on the more important aspects of the code.
Whilst this may be beneficial, the downside is that it can help perpetuate a
rather poor practice.

I hope this helps.

Anthony Borla

P.S.

There is much more to namespaces than has been discussed, so I would
recommend you do more relevant reading



Relevant Pages

  • Re: code contains problem
    ... This should not compile, you ar missing a "using namespace std;". ...
    (comp.lang.cpp)
  • Re: friend operator << overload ambiguity error
    ... > standard. ... > users to add names to namespace std under the right circumstances. ... A program may add template specializations for any standard library template ...
    (comp.lang.cpp)
  • Re: forward declarations and namespaces?
    ... I didn't even want to introduce the namespace at that point. ... non-conforming by introducing my own names into std:: This is the whole ... templateclass valarray; ... forwar declaration in there. ...
    (comp.lang.cpp)
  • Re: "using namespace std" causing soooo many errors
    ... That means you expose all of std to all files that include ... > this header file, and you end up with problems like the above. ... Using a whole namespace in similiar way with C++ standard ...
    (microsoft.public.vc.stl)
  • Re: everywhere, i need to use std::
    ... >> It's normally safe to include namespace std in a module, ... >> don't also globally include another namespace as well. ... An extern library/framework providing ... stdcli:: as needed, to make it obvious which one is being used. ...
    (comp.lang.cpp)