Topic: using namespace std;


Author: AllanW@my-dejanews.com
Date: 1998/11/10
Raw View
In article <722pho$ffg$1@shell7.ba.best.com>,
  ncm@nospam.cantrip.org (Nathan Myers) wrote:
> You would also be well-advised to put these using-declarations in
> the namespace you use for your own code, rather than at global
> scope, so they don't collide with flotsam and jetsam thrown up
> from the collisions of whatever C headers you're obliged to use.
>
> For example, C9x will have a global function "clog", despite that
> C++ has a standard stream with that name.  Stream objects names and
> function names do not overload well at all.

Theoretically, since C9X is a completely different standard than
the C++ standard, this should only be a problem for programs that
mix straight-C modules with C++ modules.

Realistically, I suspect that you're right. For platforms that
support both C and C++, the C globals may be present even for
strict C++ programs.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/11/07
Raw View
In article <MPG.10acdde590ce55349896d3@news.rmi.net>, Jerry Coffin
<jcoffin@taeus.com> writes
>If you have a compiler that makes at least some attempt at conforming
>in this area, namespace std is about like any other: if you want to
>use something in the namespace, you have to either put in a using
>namespace directive or explicitly qualify the name.  The latter is
>what I (and I think almost anybody else) would recommend, at least in
>new code.

Personally I would opt for a using declaration for common identifiers
from namespace std.  Build these up in your own headerfile and you get
the benefit of reusing it:)

so:

#ifndef MYINCL_H
#define MYINCL_H
using std::cout;
using std::cin;
using std::endl;

#endif

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


[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/11/08
Raw View
Francis Glassborow <francisG@robinton.demon.co.uk> wrote:
><jcoffin@taeus.com> writes
>>... if you want to
>>use something in the namespace, you have to either put in a using-
>>directive or explicitly qualify the name.
>
>Personally I would opt for a using-declaration for common identifiers
>from namespace std.  Build these up in your own headerfile and you get
>the benefit of reusing it:) ... so:
>
>#ifndef MYINCL_H
>#define MYINCL_H
>using std::cout;
>using std::cin;
>using std::endl;
>#endif

You would also be well-advised to put these using-declarations in
the namespace you use for your own code, rather than at global
scope, so they don't collide with flotsam and jetsam thrown up
from the collisions of whatever C headers you're obliged to use.

For example, C9x will have a global function "clog", despite that
C++ has a standard stream with that name.  Stream objects names and
function names do not overload well at all.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/11/08
Raw View
In article <722pho$ffg$1@shell7.ba.best.com>, Nathan Myers
<ncm@nospam.cantrip.org> writes
>You would also be well-advised to put these using-declarations in
>the namespace you use for your own code, rather than at global
>scope, so they don't collide with flotsam and jetsam thrown up
>from the collisions of whatever C headers you're obliged to use.

True, but the place for that is in the file doing the including else you
have gained nothing.

>
>For example, C9x will have a global function "clog", despite that
>C++ has a standard stream with that name.  Stream objects names and
>function names do not overload well at all.

Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/11/09
Raw View
In article <71vf70$fnc$1@nnrp1.dejanews.com>, AllanW@my-dejanews.com
says...

[ ... the C standard library functions being in the global namespaces
]

> I think that Visual C++ 6.0 will change this... (Hopefully they
> put in a compile switch so that we can turn this on for new
> programs, and off for backwards compatibility...) Can anyone comment?

Yes, I can comment.  VC++ 6 does not fix 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: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/11/09
Raw View
"Michel Michaud" <micm19@removethis.mail2.cstjean.qc.ca> writes:

>Steve Clamage wrote <71qtlv$sl1$1@engnews1.eng.sun.com>...
>>Neither. std is a namespace like any other, except that the name
>>is reserved, and you are not allowed to add your own contents
>>to it.

>What about

>namespace std {}

>is this "adding to it".

That's ok. The restriction is that you cannot add declarations or
definitions to namespace std (except for template specializations).
You can declare the namespace itself.

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


[ 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: william.kempf@firstdatacorp.com
Date: 1998/11/06
Raw View
In article <71sqr1$tdl$1@nnrp1.dejanews.com>,
  jkanze@otelo.ibmmail.com wrote:
>
> In article <71qtlv$sl1$1@engnews1.eng.sun.com>,
>   clamage@Eng.Sun.COM (Steve Clamage) wrote:
>
> > >GCC behaves differently than MS Visual C++ in this regard, so I'm
> > >confused.
> >
> > MS VC++ is not standard-conforming in this regard. It does not
> > put anything in namespace std. At least not yet.
>
> Are you sure.  I thought it was the other way around: g++ doesn't support
> namespaces, but VC++ (at least version 5.0) does, and the new version of
> the library has all of the C++ parts (but not the headers inherited from
> C) in namespace std.

VC++ 5 and up put the C++ files in namespace std.  C header files ending in
.h are not placed in std, nor are they supposed to be AFAIK.  The C++
alternatives (such as <cstring> for <string.h>) do place everything in std.
However, at least in VC++ 5 they do a bad job of undefining macros to use
full function implementations, leaving some commands to appear to be in the
global namespace.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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: stephen.clamage@Eng.sun.com (Steve Clamage)
Date: 1998/11/06
Raw View
william.kempf@firstdatacorp.com writes:

>VC++ 5 and up put the C++ files in namespace std.  C header files ending in
>.h are not placed in std, nor are they supposed to be AFAIK.

Section D.5, "Standard C library headers":

"Each C header, whose name has the form name.h, behaves as if each name
placed in the Standard library namespace by the corresponding cname
header is also placed within the namespace scope of the namespace std
and is followed by an explicit using-declaration (7.3.3)"

In other words, if you include one of the ".h" forms of the C
library headers, you are supposed to find the names of all
types, objects, and functions in namespace std and also in
the global namespace.

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


[ 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: AllanW@my-dejanews.com
Date: 1998/11/07
Raw View
In article <71sqr1$tdl$1@nnrp1.dejanews.com>,
  jkanze@otelo.ibmmail.com wrote:
>
> In article <71qtlv$sl1$1@engnews1.eng.sun.com>,
>   clamage@Eng.Sun.COM (Steve Clamage) wrote:
>
> > >GCC behaves differently than MS Visual C++ in this regard, so I'm
> > >confused.
> >
> > MS VC++ is not standard-conforming in this regard. It does not
> > put anything in namespace std. At least not yet.
>
> Are you sure.  I thought it was the other way around: g++ doesn't support
> namespaces, but VC++ (at least version 5.0) does, and the new version of
> the library has all of the C++ parts (but not the headers inherited from
> C) in namespace std.

You're both right.

Visual C++ 5.0 does support namespaces. However, since the portions
of the standard library that are inherited from C are in the global
namespace, instead of namespace std, you can't write your programs
as if you were using a fully-compliant compiler:
    #include <cstdio>
    int main() { std::printf("Hello"); }
This does not work because printf() is in the global namespace,
not namespace std. (After removing the "std::" this works fine.)

I think that Visual C++ 6.0 will change this... (Hopefully they
put in a compile switch so that we can turn this on for new
programs, and off for backwards compatibility...) Can anyone comment?

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ 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: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/11/07
Raw View
In article <ljd874fwnl.fsf@krusty.e-technik.uni-dortmund.de>,
mvo@krusty.e-technik.uni-dortmund.de says...

[ ... ]

> what is the word on using the namespace `std'?  Do I have to
> explicitely put a using clause in my sources whenever I want to use a
> feature from the standard library, or is the namespace `std' supposed
> to be `open' always?
>
> GCC behaves differently than MS Visual C++ in this regard, so I'm
> confused.

GCC has a few hacks in the parser to let it basically read and ignore
some things related to namespaces, but has virtually no real namespace
support.  If you want to use namespaces, you almost certainly want to
upgrade to egcs -- I'm not sure it's namespace support is exactly
right, but it's at least close enough for most purposes.

The standard library is a bit different situation: a few parts of the
library included with egcs are reasonably close to conforming (mostly
the stl portions are fairly close) but even the parts that are close
are NOT in namespace std.

If you have a compiler that makes at least some attempt at conforming
in this area, namespace std is about like any other: if you want to
use something in the namespace, you have to either put in a using
namespace directive or explicitly qualify the name.  The latter is
what I (and I think almost anybody else) would recommend, at least in
new code.
---
[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/11/07
Raw View
<william.kempf@firstdatacorp.com> wrote:
>  jkanze@otelo.ibmmail.com wrote:
>> I thought it was the other way around: g++ doesn't support
>> namespaces, but VC++ (at least version 5.0) does, and the new version of
>> the library has all of the C++ parts (but not the headers inherited from
>> C) in namespace std.
>
>VC++ 5 and up put the C++ files in namespace std.  C header files ending in
>.h are not placed in std, nor are they supposed to be AFAIK.

The C header files like "stdio.h" are supposed to do exactly the same
thing as the "cstdio" headers, and *additionally* alias the names
they define into the global scope, e.g.

  using std::FILE;
  using std::fprintf;

The global name has to define the same object as the one in std::.
The reason this makes a difference is that Koenig lookup must
find functions declared in std:: if you pass a type defined in
std::, and the C types like FILE and tm are required to be
defined in std::.

Some implementers maintain that it's OK for these headers to
define the C names globally and then alias them in std::, but
I can't find any support for that in the standard.

>The C++
>alternatives (such as <cstring> for <string.h>) do place everything in std.
>However, at least in VC++ 5 they do a bad job of undefining macros to use
>full function implementations, leaving some commands to appear to be in the
>global namespace.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/
---
[ 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: Marius Vollmer <mvo@krusty.e-technik.uni-dortmund.de>
Date: 1998/11/03
Raw View
Hi,

what is the word on using the namespace `std'?  Do I have to
explicitely put a using clause in my sources whenever I want to use a
feature from the standard library, or is the namespace `std' supposed
to be `open' always?

GCC behaves differently than MS Visual C++ in this regard, so I'm
confused.

thanks,
 Marius


[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/11/05
Raw View
Marius Vollmer <mvo@krusty.e-technik.uni-dortmund.de> writes:

>what is the word on using the namespace `std'?

In a standard-conforming implementation, the entire standard
library is in namespace std. (Except for the various versions
of operator new and operator delete, which are in the global
namespace.)

>Do I have to
>explicitely put a using clause in my sources whenever I want to use a
>feature from the standard library, or is the namespace `std' supposed
>to be `open' always?

Neither. std is a namespace like any other, except that the name
is reserved, and you are not allowed to add your own contents
to it. You don't need using-declarations or using-directives
to access the standard library, although you can employ them if
you want.  Example:

#include <iostream>
int main() { std::cout << "Hello, world!" << std::endl; }

>GCC behaves differently than MS Visual C++ in this regard, so I'm
>confused.

MS VC++ is not standard-conforming in this regard. It does not
put anything in namespace std. At least not yet.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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: James Kuyper <kuyper@wizard.net>
Date: 1998/11/05
Raw View
Steve Clamage wrote:
>
> Marius Vollmer <mvo@krusty.e-technik.uni-dortmund.de> writes:
>
> >what is the word on using the namespace `std'?
>
> In a standard-conforming implementation, the entire standard
> library is in namespace std. (Except for the various versions
> of operator new and operator delete, which are in the global
> namespace.)
>
> >Do I have to
> >explicitely put a using clause in my sources whenever I want to use a
> >feature from the standard library, or is the namespace `std' supposed
> >to be `open' always?
>
> Neither. std is a namespace like any other, except that the name
> is reserved, and you are not allowed to add your own contents
> to it.

Not quite. You're allowed to add a specialization of a standard template
to std, so long as it depends upon a user-defined name of external
linkage, and meets all the requirements of the standard for that
template.

As footnote 163 makes clear, this has the side effect of limiting how
much "compiler magic" the implementation can put into the standard
templates. They are required to be written in such a way that they can
interact with user-defined templates that meet only the minimum
requirements. Even though an implementation has written a template with
certain implementation-specific features, the implementation is not
allowed to assume that those features exist when using that template in
another section of code. The exception, of course, is when using a
template instantiation that cannot depend upon user-defined names with
external linkage.


[ 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: "Larry Brasfield" <larry_br@sea_net.com>
Date: 1998/11/05
Raw View
Steve Clamage wrote in message <71qtlv$sl1$1@engnews1.eng.sun.com>...
>Marius Vollmer <mvo@krusty.e-technik.uni-dortmund.de> writes:
[Q re std namespace cut.]
>>GCC behaves differently than MS Visual C++ in this regard, so I'm
>>confused.
>
>MS VC++ is not standard-conforming in this regard. It does not
>put anything in namespace std. At least not yet.

False.  For nearly 2 years, since the release of Microsoft
Visual C++ v5, the standard library names have been
declared in namespace std when headers are included
via the standard names.  (#include <string>, for example.)

For reasons of backward compatibility, VC5 and VC6
have allowed the old header names to be included,
which puts names in the global namespace.  Perhaps
this behavior is what leads to Mr. Clamage's mistake.

--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.com )




[ 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: jkanze@otelo.ibmmail.com
Date: 1998/11/05
Raw View
In article <71qtlv$sl1$1@engnews1.eng.sun.com>,
  clamage@Eng.Sun.COM (Steve Clamage) wrote:

> >GCC behaves differently than MS Visual C++ in this regard, so I'm
> >confused.
>
> MS VC++ is not standard-conforming in this regard. It does not
> put anything in namespace std. At least not yet.

Are you sure.  I thought it was the other way around: g++ doesn't support
namespaces, but VC++ (at least version 5.0) does, and the new version of
the library has all of the C++ parts (but not the headers inherited from
C) in namespace std.

--
James Kanze                         email: kanze@gabi-soft.fr

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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: "Michel Michaud" <micm19@removethis.mail2.cstjean.qc.ca>
Date: 1998/11/05
Raw View
Steve Clamage wrote <71qtlv$sl1$1@engnews1.eng.sun.com>...
>Neither. std is a namespace like any other, except that the name
>is reserved, and you are not allowed to add your own contents
>to it. You don't need using-declarations or using-directives

What about

namespace std {}

is this "adding to it".

I use this with VC to be able to write using namespace std after
including standard files like <cstring> or <cmath> because they
don't use the std namespace. So without a using my file won't
compile in the future, but without my empty addition, I get
an error on the using ("std is not a typename"). I guess from
your comment that I am doing the best I can do, because

namespace std
{
#include <cmath>
#include <cstring>
}

would be illegal (it does add to std), and will be incorrect
in the future when the files put their names in std.

Michel Michaud micm19@removethis.mail2.cstjean.qc.ca
http://www3.sympatico.ca/michel.michaud
---
[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/11/06
Raw View
<jkanze@otelo.ibmmail.com> wrote:
>  clamage@Eng.Sun.COM (Steve Clamage) wrote:
>> MS VC++ is not standard-conforming in this regard. It does not
>> put anything in namespace std. At least not yet.
>
>Are you sure.  I thought it was the other way around: g++ doesn't support
>namespaces, but VC++ (at least version 5.0) does, and the new version of
>the library has all of the C++ parts (but not the headers inherited from
>C) in namespace std.

The Egcs-1.1 g++ does support namespaces.  The library that comes with
it does not put anything in std, yet.  MSVC++ has some partial support
for namespaces, and it puts names defined in the non-".h" headers in std.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



[ 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              ]