How to say: NOT using namespace std
flag
Messages 65 - 74 of 217 - Collapse all
/groups/adfetch?hl=en&adid=9QQXoxIAAAC7_58w_XSXN-iGC36ZzLLRxyxpo7bMRVgpn9ZMKisPfQ
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  Esa  
View profile  
 More options Jun 12 2001, 10:01 am
Newsgroups: comp.lang.c++.moderated
From: "Esa" <x...@y.z>
Date: 12 Jun 2001 10:01:18 -0400
Local: Tues, Jun 12 2001 10:01 am
Subject: How to say: NOT using namespace std
I have defined in my code in the global scope:

using namespace std;

How do I turn it off, i.e. that I am not using namespace std anymore?

/Esa

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
2.  Fehér  
View profile  
 More options Jun 12 2001, 1:08 pm
Newsgroups: comp.lang.c++.moderated
From: Fehér...@ronto.lmf.ericsson.se,
Date: 12 Jun 2001 12:56:43 -0400
Local: Tues, Jun 12 2001 12:56 pm
Subject: Re: How to say: NOT using namespace std
Esa wrote:

> I have defined in my code in the global scope:

> using namespace std;

> How do I turn it off, i.e. that I am not using namespace std anymore?

[SNIP]

There is no way.  Don't use using namespace std; - it is evil.  Name
specifically those functions/types/classes/etc you want to use.  Also
try to name them with using _inside_ you function bodies.  Outside use
std:: (like for return values).  Don't pollute the global namespace if
you don't have to.

A

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
3.  Jon Bell  
View profile  
 More options Jun 12 2001, 1:08 pm
Newsgroups: comp.lang.c++.moderated
From: jtb...@presby.edu (Jon Bell)
Date: 12 Jun 2001 12:57:04 -0400
Local: Tues, Jun 12 2001 12:57 pm
Subject: Re: How to say: NOT using namespace std

In article <f0gV6.1$22....@news.get2net.dk>, Esa <x...@y.z> wrote:
>I have defined in my code in the global scope:

>using namespace std;

>How do I turn it off, i.e. that I am not using namespace std anymore?

Simply remove that statement.  Then if you want to use any names from std,
you must either (a) prefix them with 'std::', for example:

    std::cout << "Hello, world!" << std::endl;

or (b) name them in 'using' declarations:

    using std::cout;
    using std::endl;

--
Jon Bell <jtb...@presby.edu>                        Presbyterian College
Dept. of Physics and Computer Science        Clinton, South Carolina USA

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
4.  Radoslav Getov  
View profile  
 More options Jun 12 2001, 1:09 pm
Newsgroups: comp.lang.c++.moderated
From: "Radoslav Getov" <nos...@mai.com>
Date: 12 Jun 2001 13:00:21 -0400
Local: Tues, Jun 12 2001 1:00 pm
Subject: Re: How to say: NOT using namespace std
"Esa" <x...@y.z> wrote in message news:f0gV6.1$22.962@news.get2net.dk...

: I have defined in my code in the global scope:
:
: using namespace std;
:
: How do I turn it off, i.e. that I am not using namespace std anymore?
:

Impossible, but there is a workaround and advice: *never* use 'using
namespace' (IMHO 'using' shouldn't exist at all).

Radoslav Getov

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
5.  Thomas Kunert  
View profile  
 More options Jun 13 2001, 6:43 pm
Newsgroups: comp.lang.c++.moderated
From: Thomas Kunert <kun...@physik.tu-dresden.de>
Date: 13 Jun 2001 18:43:06 -0400
Local: Wed, Jun 13 2001 6:43 pm
Subject: Re: How to say: NOT using namespace std
Radoslav Getov wrote:

> Impossible, but there is a workaround and advice: *never* use 'using
> namespace' (IMHO 'using' shouldn't exist at all).

What's wrong with it? In most cases it seems quite comfortable to write

using namespace std;

at the beginning of each source file that uses features from the
standard library. Of course, you might have problems if you have classes
in other namespaces with the same name like standard library classes,
but do people really do that?

Regards,
Thomas Kunert

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
6.  Francis Glassborow  
View profile  
 More options Jun 14 2001, 1:05 am
Newsgroups: comp.lang.c++.moderated
From: Francis Glassborow <francis.glassbo...@ntlworld.com>
Date: 14 Jun 2001 01:05:05 -0400
Local: Thurs, Jun 14 2001 1:05 am
Subject: Re: How to say: NOT using namespace std
In article <3B275B62.A96F...@physik.tu-dresden.de>, Thomas Kunert
<kun...@physik.tu-dresden.de> writes

>Radoslav Getov wrote:

>> Impossible, but there is a workaround and advice: *never* use 'using
>> namespace' (IMHO 'using' shouldn't exist at all).

>What's wrong with it? In most cases it seems quite comfortable to write

>using namespace std;

>at the beginning of each source file that uses features from the
>standard library. Of course, you might have problems if you have classes
>in other namespaces with the same name like standard library classes,
>but do people really do that?

Yes, but in addition solutions should apply to other libraries and
'using directives are far too heavy.

I would very much like to be able to write (particularly in header
files) using namespace xyz at the beginning and ~using namespace xyz at
the end. But I would also like to be able to write:

using namespace xyz;
~using xyz::feature;

Now my question is 'how much more complicated does this make compiler
writing?

Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7.  Attila Feher  
View profile  
 More options Jun 15 2001, 7:30 am
Newsgroups: comp.lang.c++.moderated
From: Attila Feher <Attila.Fe...@lmf.ericsson.se>
Date: 15 Jun 2001 07:30:54 -0400
Local: Fri, Jun 15 2001 7:30 am
Subject: Re: How to say: NOT using namespace std
Francis Glassborow wrote:

[SNIP]
> Yes, but in addition solutions should apply to other libraries and
> 'using directives are far too heavy.

> I would very much like to be able to write (particularly in header
> files) using namespace xyz at the beginning and ~using namespace xyz at
> the end. But I would also like to be able to write:

> using namespace xyz;
> ~using xyz::feature;

> Now my question is 'how much more complicated does this make compiler
> writing?

[SNIP]

I suggets you do write it only to function bodies etc. where is it
"scoped".  In the declarations take the time to fully qualify your
references, like:

std::string Class::cnvStr( const std::string & s, const std::string
&cnvlist) {
  using std::string;
  using std::whatever;
  // ...

}

A

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
8.  Steve Clamage  
View profile  
 More options Jun 14 2001, 12:59 am
Newsgroups: comp.lang.c++.moderated
From: Steve Clamage <clam...@eng.sun.com>
Date: 14 Jun 2001 00:59:02 -0400
Local: Thurs, Jun 14 2001 12:59 am
Subject: Re: How to say: NOT using namespace std
On 12 Jun 2001, Radoslav Getov wrote:

> "Esa" <x...@y.z> wrote in message news:f0gV6.1$22.962@news.get2net.dk...
> : I have defined in my code in the global scope:
> :
> : using namespace std;
> :
> : How do I turn it off, i.e. that I am not using namespace std anymore?
> :

> Impossible, but there is a workaround and advice: *never* use 'using
> namespace' (IMHO 'using' shouldn't exist at all).

A using-directive might be OK if you restrict its scope. For
example, you might create auxiliary namespaces for use in
a primary namespace. Inside the primary namespace,
        using namespace auxiliary;
is probably fine.

But I agree that you never should put
        using namespace std;
in global scope, or probably anywhere. It is too big a hammer,
introducing names you probably do not expect.

Unfortunately, many example programs in many textbooks
employ such a using-declaration. I don't know why. I would
steer clear of such books, as they probably contain other
bad advice.

Even in a toy program it is a simple matter to add a using-
directive for just the names you intend to use:

#include <iostream>
using std::cout;
using std::endl;

int main()
{
    cout << "Hello, world!" << endl;

}

If you forget one, the compiler will remind you. :-)

---
Steve Clamage, stephen.clam...@sun.com

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
9.  Jon Bell  
View profile  
 More options Jun 14 2001, 11:47 am
Newsgroups: comp.lang.c++.moderated
From: jtb...@presby.edu (Jon Bell)
Date: 14 Jun 2001 11:47:02 -0400
Local: Thurs, Jun 14 2001 11:47 am
Subject: Re: How to say: NOT using namespace std
In article <Pine.SOL.3.96.1010612125722.12156D-100000@taumet>,
Steve Clamage  <clam...@eng.sun.com> wrote:

>But I agree that you never should put
>    using namespace std;
>in global scope, or probably anywhere. It is too big a hammer,
>introducing names you probably do not expect.

>Unfortunately, many example programs in many textbooks
>employ such a using-declaration. I don't know why.

In many cases, the authors probably did it as a cheap way to make their
code from an earlier edition compatible with the standard.  It's surely
rather time-consuming to add 'std::'s to a lot of pre-existing code, and
then check to make sure each program still compiles.

--
Jon Bell <jtb...@presby.edu>                        Presbyterian College
Dept. of Physics and Computer Science        Clinton, South Carolina USA

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
10.  quux111  
View profile  
 More options Jun 14 2001, 8:16 pm
Newsgroups: comp.lang.c++.moderated
From: quux...@newsguy.com (quux111)
Date: 14 Jun 2001 20:16:09 -0400
Local: Thurs, Jun 14 2001 8:16 pm
Subject: Re: How to say: NOT using namespace std
jtb...@presby.edu (Jon Bell) wrote in news:GEwrJs.Dnr@presby.edu:

Putting std:: in front of simple pieces of code is needlessly messy,
especially when a given code fragment uses only std:: elements.  It's a
fair practice if you write tutorial code -- I do it all the time in my
documentation, but never in production code.

I do agree that "using namespace blah;" is bad practice *in most cases* and
should be avoided.

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google