using namespace std;

using namespace std;

Post by Michael J. » Tue, 18 May 2004 22:53:22



A recent text suggest that C++ has only recently been
standardized???

That to use standard functions, you must include "using
namespace std;" in your program, or else use a statement indicates to
use the std form.

With "using namespace std", an output statement would appear as:

cout<<......

Without, it would appear as:

std::cout<<.....

I find this to be a bit confusing. Could someone explain the
background for this duplicity???

Also, is this standard among compilers or optional among the
publishers of C++ software???

THANKS.

MJR
Michael J. Reeves, AA, ASc
E-Mail: XXXX@XXXXX.COM
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by Sharad Kal » Wed, 19 May 2004 05:43:46


That's 1998. The standard with technical corrigendum is ISO/IEC 14882:2003.


Pre-standard headers were like <iostream.h>. The standardization made the
standard headers extensionless like <iostream>. Also everything inside the
headers was wrapped inside the std namespace. Hence cout is actually std::cout
or else you have to write "using namespace std", which brings the names of std
namespace into current scope.



It's not optional for any compiler conforming to the C++ standard.

-Sharad



[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
 
 

using namespace std;

Post by Andrew Koe » Wed, 19 May 2004 05:45:01


The standard was ratified in 1997 and became official in 1998.


That's two misstatements; perhaps you should consider a different text.


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by Jeff Schwa » Wed, 19 May 2004 23:41:24


1998


The statement "using namespace std;" does not tell the compiler to use
any particular form of C++, standard or otherwise. Elements of the
standard libraries are stored in a namespace called std; there are
several benefits to this approach.


If you'll stop calling it duplicity.


It's specified in the ISO standard, which can be downloaded in PDF form
for USD 18. Any degree of compliance with the standard is optional.

FYI, it's not necessary to use three question marks every time you ask a
question. One mark will serve.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by llewell » Wed, 19 May 2004 23:43:25

"Michael J. Reeves, AA, ASc" < XXXX@XXXXX.COM > writes:


C++ was standardized in 1998. Perhaps the text was less recent than
you thought? Or, perhaps, like certain writers (ESR, to name
one), the author misconstrued the 2003 TC as major changes, when
in fact it consists entirely of minor bugfixes.


This is part of the story.

The C++ standard library contains many, many names. It is unlikely
any one person knows them all. Some of them may conflict with
names you may wish to use for your program. For example, 'count'
is a standard C++ library algorithm, provided by the header
<algorithm>.

To allow C++ programs to use these names for their own programs, the
standard C++ library names are wrapped in a namespace, which is
named 'std'.

For example, in the header <cmath> there is the equivalent of:

namespace std
{
double sin(double);
}

This results in 'sin' having the fully qualified name
std::sin .

A using declaration:

using std::sin;

allows you to refer to std::sin using 'sin'.

A using directive:

using namespace std;

allows you to refer to all names in namespace std by their
unqualified form.

Namespaces are discussed in detail in the archives, and in any good
C++ book published since about 1997, such as Stroustrup's _The C++
Programming Language_, 3rd Edition, or Koenig & Moo's
_Accelerated C++_ .



You are misuing the word 'duplicity'. Please look it up in a
dictionary.


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by Early Ehli » Thu, 20 May 2004 00:21:39


Recency is subjective, of course...

IIRC, the standard became a Standard in 1998. A .pdf copy is available
somewhere on the ANSI web site for something like $15.00.

The notion of namespace std is somewhat less recent than the standard.

The notion of namespace is somewhat less recent than that.


To understand the "using namespace std" vs. "std::", you need to understand
the notion of namespaces. Very briefly, they are somewhat analogous to
"directories" in a filesystem. You can put portions of your code into their
own namespaces, like so:

namespace my_namespace
{
void foo( );
}

namespace your_namespace
{
void foo( );
}

We've now defined two functions named "foo." The first is
my_namespace::foo( ), the second is your_namespace::foo( ).

This is useful, for example, if you and I are working on a large project
where we are concerned about having each of us write functions that conflict
with each other. If we decide beforehand that everything I write will go in
my_namespace::, and everything you write will go in your_namespace::, then
we can write our code farely freely without having to worry that we'll
produce a conflict.

In this spirit of cooperation, the C++ Standards committee decided to put
the entire Standard C++ Library into namespace std. This is why you need to
specify "which" cout you want.

There are three ways to specify which cout you want. The first is to use
std::cout. The second is to place a "using namespace std" before your first
reference to cout. The final is to place a "using std::cout" before your
first reference to cout.

The first approach does not alter what is visible in the current scope,
while the second two will. The "using namespace..." approach takes
*everything* that is currently defined in the specified namespace, and makes
it visible in the current scope. The "using std::cout" takes only std::cout
and makes it visible in the current scope.

The approach that you take is largely a matter of personal preference,
style, etc. I prefer to never *ever* use the "using namespace" approach,
because it can have unexpected results. Do a groups.google.com search on
"using namespace" and you'll likely find tons of reading material on the
subject.


This is specified by the ISO C++ Standard (aka ANSI C++). AFAIK, no
compiler is yet 100% compliant with the Standard. However, any
recently-released compiler ought to have at *least* this feature.

--
Best Regards,
- Early Ehlinger - CEO - www.ResPower.com - 866-737-7697
- 500+ GHz Self-Service Super-Computer from $0.50/GHz*Hr
- (yes, five hundred gigahertz. point-five terahertz.)



[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by dtmoor » Thu, 20 May 2004 02:26:53


The first ANSI/ISO C++ Standard came out in 1998 .. I suppose this
could be seen as recently in some contexts, but probably not when
dealing with computer languages ... when was your book published?


Using directives, like "using namespace std", are evil ... they remove
explicit qualifications from great wodges of names, potentially
resulting in widespread name-collisions, not to mention intestinal
distress and *** -onset diabetes. Besides, the whole point of
namespaces is to avoid precisely these name-collision issues.


It is called name-qualification and it is 'a good thing' .. the
facilities provided by the Standard Library are provided in a
namespace called "std". So, if you want to use one, you need to
qualify the name with the scope-resolution operator (::), to tell the
compiler that you want to use the name supplied in std, and not in
My_Crazy_Uncle_Bills_Namespace ... which may also be defined in your
program. For more info, look up namespaces in your favorite C++ text
... although perhaps not the one you seem to be using, because it
seems to be a bit dated ... I recommend going to the source .. pick up
a copy of "The C++ Programming Language" 3rd edition, by Bjarne
Stroustrup.


It IS most definitely specified in the Standard ... but for (what
seemed to me like) a long time, many commercial compilers seemed to
consider it optional anyway ... things are generally better now, and
even Microsoft is starting to improve its compliance to the C++
Standard ... now if only they'd shoot those MFC classes off into space
...

HTH, Dave Moore

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by Rob » Thu, 20 May 2004 06:48:02


> A recent text suggest that C++ has only recently been
> standardized???

Depends on definition of "recent", I guess. C++ was standardised
in 1998.

>
> That to use standard functions, you must include "using
> namespace std;" in your program, or else use a statement indicates to
> use the std form.
>
> With "using namespace std", an output statement would appear as:
>
> cout<<......
>
> Without, it would appear as:
>
> std::cout<<.....
>
> I find this to be a bit confusing. Could someone explain the
> background for this duplicity???

It's not duplicity. Objects and functions in the standard C++
library exist in a namespace called std. The reason for namespaces
is that they allow use of multiple libraries within one program,
even if some of the names are the same between libraries.
Consider, for example, another library that has an object named
cout. By placing it in a namespace named not_std (for example)
both versions of cout can be used.

std::cout << "Hello\n";
not_std::cout.SomeOperation();

The using namespace std directive simply means that std::cout
can be used directly, without prefixing it with std::.

Namespace std is special, in the sense that its contents (what
types, what objects, etc) are defined in the C++ standard.


>
> Also, is this standard among compilers or optional among the
> publishers of C++ software???
>

Namespaces are part of standard C++, and standard C++ library
functions are required by the standard to be in namespace std.
This is required of all compliant compilers and code that is expected
to compile and work correctly with those compilers.



[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by kanz » Thu, 20 May 2004 07:22:39


> "Michael J. Reeves, AA, ASc" < XXXX@XXXXX.COM > wrote in


> > A recent text suggest that C++ has only recently been
> > standardized???

> That's 1998. The standard with technical corrigendum is ISO/IEC
> 14882:2003.

Well, recent is relative:-). It's recent compared to when I started
using C++, but it IS old enough that one could expect most compilers to
be conformant.

> > That to use standard functions, you must include "using namespace
> > std;" in your program, or else use a statement indicates to use the
> > std form.

> > With "using namespace std", an output statement would appear as:

> > cout<<......

> > Without, it would appear as:

> > std::cout<<.....

> > I find this to be a bit confusing. Could someone explain the
> > background for this duplicity???

> Pre-standard headers were like <iostream.h>.

Pre-standard headers varied:-(. Although <iostream.h> was probably the
most common, I think that there were also <iostream.hpp>. And of
course, I've had to use <strstrea.h> in place of <strstream.h>

> The standardization made the standard headers extensionless like
> <iostream>. Also everything inside the headers was wrapped inside the
> std namespace. Hence cout is actually std::cout or else you have to
> write "using namespace std", which brings the names of std namespace
> into current scope.

The standard defined a new set of headers, with only a tenuous
relationship to <iostream.h>. While you can generally convert simple
uses from the classic <iostream.h> to the new headers (<ostream>,
<istream>, etc.) fairly trivially, there are a number of traps.

> > Also, is this standard among compilers or optional among the
> > publishers of C++ software???

> It's not optional for any compiler conforming to the C++ standard.

But how many compilers conform?

In fact, most recent compilers conform with regards to this point. But
compiler vendors are still pretty much using the standard like you would
a supermarket, and only picking up what they want from it, as though the
entire standard were just a collection of optional features.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by fmdf » Thu, 20 May 2004 18:45:05


...
...
Most of the people who have already post a follow-up in this thread
didn't say to you that you can use namespaces for your convenience
too.
You can create your own namespaces in which you can put your own stuff
naming the members with the names you want without any regard to C++
members of the std namespace.

Just use in mynamespace.h:

namespace MyNameSpace
{
class Class
{...};
struct Struct
{...};
void Funct();
...
}

Then you can, in your main.cpp:

...
#include "mynamespace.h"
...
using namespace std;
...
int main()
{
using MyNameSpace::Funct;
using MyNameSpace::Class; // or you can write:
using MyNameSpace::Struct; // using namespace MyNameSpace;
...
Funct(); // referring to MyNameSpace::Funct();
::Funct(); // referring to std::Funct();
std::Funct(); // Also referring to std::Funct();
...
}

Ciao,

Fabio De Francesco.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by Michael J. » Fri, 21 May 2004 11:18:32

THANKS for the good responses.

Just to rehash and make sure I've got the just of it...

Prior to 1998, there was no official (ISO/ANSI) standard for the
C++ language. (I thought there was some sort of standard, and that the
standard was modified to a more current version as has been done with C,
FORTRAN, et al?)
This explains a lot regarding different versions of compilers
even within the same publisher, and the semantics/syntax incorporated.

Having a "using directive" (using namespace std;) can lead to
call collisions when the calls refer to the same function name, but are
in different libraries.

Good programming practice would suggest using: std::cout and
my_library::cout to avoid collisions during compilation.

I suspect that placing a statement preceding the call as
example:
using std;
cout<<...;
:
using my_library;
cout<<...;
might still result in call collisions during compilation
depending on how the preprocessor and compiler are implemented?

BTW, I am working through:
Programming and Problem Solving with C++, 3rd Ed;
and
A Laboratory Course in C++, 3rd Ed;

Both have major changes from the 1st editions I used in the
mid-90's.

Of course, that also applies to Bjourne's, The C++ Programming
Language, 1st Ed and the new one I bought, Special Edition.

Any other insights that I might benefit from?

Again, THANKS.

MJR
Michael J. Reeves, AA, ASc
E-Mail: XXXX@XXXXX.COM
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by kanz » Fri, 21 May 2004 18:59:39


> FYI, it's not necessary to use three question marks every time you ask
> a question. One mark will serve.

It's training for trigraphs.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by P.J. Plaug » Fri, 21 May 2004 19:47:36


> > It's not optional for any compiler conforming to the C++ standard.
>
> But how many compilers conform?
>
> In fact, most recent compilers conform with regards to this point. But
> compiler vendors are still pretty much using the standard like you would
> a supermarket, and only picking up what they want from it, as though the
> entire standard were just a collection of optional features.

That's the regrettable effect of an overly ambitious standard:

1) Compiler vendors are driven primarily by customer demand.

2) There's always *way* more features to add than time to do it in.

3) Complete conformance *never* rates at the top of the weighted
list of customer demands produced by marketing/customer support.

Thus, in the early stages of every version planning meeting I've
ever hosted, attended, or been privy to, one of the first questions
asked by the top decision maker is, "Can we get complete conformance
with this release?" If the answer is no, then the obvious fallback
is what you so accurately characterize as the supermarket model --
pick and choose the features that have the best short-term payoff.

"If you standardize it, they will come" is a mantra I've heard
way too often in standards meetings. True, putting a feature in
a standard has a certain amount of coercive power. But every
standard that overreaches suffers an almost catastrophic collapse
in its ability to coerce uniform behavior. Try to demand too much
and you might not get even the things you thought were a slam
dunk. No vendor will run an ad claiming "not quite complete
conformance, but we've done a great job on all the really important
parts." Doesn't sell compilers.

P.J. Plauger
Dinkumware, Ltd.
http://www.yqcomputer.com/


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by llewell » Sat, 22 May 2004 02:01:27


XXXX@XXXXX.COM writes:




:-)

Or maybe it's a trigraph trap. People using repeated question marks
in string literals or comments occasionally get unexpected
output. In many enviroments it seems desireable to have a coding
standard that outlaws sequences of more than one '?'.




[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

using namespace std;

Post by Jonathan L » Sat, 22 May 2004 20:04:38


For really bad examples of over-reaching standards leading to nobody
implementing them, consider either SQL (1992, 1999, 2003), or the OSI
standard (remember that?).

--
Jonathan Leffler #include <disclaimer.h>
Email: XXXX@XXXXX.COM , XXXX@XXXXX.COM
Guardian of DBD::Informix v2003.04 -- http://www.yqcomputer.com/

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]