Topic: What is the standard for STL classes in 'std' namespace?
Author: sirwillard@my-dejanews.com
Date: 1999/04/13 Raw View
In article <7elqri$5bc@abyss.West.Sun.COM>,
stanley@west.sun.com (Stanley Friesen [Contractor]) wrote:
> In article <370e0ec5.3665495@news.ccnet.com>,
> Sounds like legacy code.
>
> For that, "using namespace std;" is probably the best approach. One useful
> shortcut is to put this in a private header file that also includes the
> standard header(s), and then use that wrapper instead of the standard header
> in all your source files.
I don't agree. "using namespace std;" is probably NEVER the best approach.
It defeats the whole purpose of namespaces. A better approach would be
something along the lines of:
#ifdef COMPILER_HAS_NAMESPACES
#define STD std::
#else
#define STD
#endif
STD vector<int> v;
Now you retain the benefit of namespace on compilers that are conformant (and
I'd recommend doing the main chunk of coding on such a compiler), while
allowing non-conformant compilers to compile as well.
-----------== 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: James Kuyper <kuyper@wizard.net>
Date: 1999/04/15 Raw View
Siemel Naran wrote:
....
> If you say
> #include <vector>
> then you must say
> std::vector<int> v;
> Try to use this method if possible as you get the full
> benefit of namespaces.
>
> If you say
> #include <vector.h>
> then you must say
> vector<int> v;
Wrong - first, there is no <vector.h> in the standard library. The files
which are available in both <name.h> versions and <cname> versions are
only the library headers carried over from C, not the C++ additions. Of
course, older compilers may have used <vector.h>, and newer releases may
still support it for backwards compatibility, but that is a non-standard
extension, and hence outside the bounds of this discussion.
Secondly, for the C standard library headers, the <name.h> versions load
their symbols both into the global namespace and into the std
namespace. Again, many older compilers may not implement that feature
yet, but that means they don't yet conform to the standard.
Therefore, the following code is perfectly legal:
#include <assert.h>
#include <math.h>
int comp = (sin == std::sin);
---
[ 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.Kanze@dresdner-bank.com
Date: 1999/04/13 Raw View
In article <slrn7gqebf.7au.sbnaran@bardeen.ceg.uiuc.edu>,
sbnaran@KILL.uiuc.edu wrote:
>
> If you say
> #include <vector.h>
> then you must say
> vector<int> v;
If you say
#include <vector.h>
then you are not writing standard C++, and the rest depends on your
implementation.
> Try not to use this method if possible as it zeroes out
> the benefits of namespaces. You might need to use this
> method as a transition tool. Say you have lots of old
> C++ code that doesn't use namespaces. So then you'd
> write "#include <vector.h>". But later on, when you
> have more time, you'd change this to "#include <vector>"
> and change usage of "vector<int>" to "std::vector<int>".
> A future revision of C++ might throw out the header
> files with the .h suffix.
Difficult to throw out something that isn't there.
--
James Kanze mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orient e objet/
Beratung in objekt orientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49 (069) 63 19 86 27
-----------== 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: stanley@west.sun.com (Stanley Friesen [Contractor])
Date: 1999/04/13 Raw View
In article <7etfdt$5qi$1@nnrp1.dejanews.com>,
<sirwillard@my-dejanews.com> wrote:
>> For that, "using namespace std;" is probably the best approach. One useful
>> shortcut is to put this in a private header file that also includes the
>> standard header(s), and then use that wrapper instead of the standard header
>> in all your source files.
>
>I don't agree. "using namespace std;" is probably NEVER the best approach.
Maybe I should have said "the only viable approach". When, as in this
case, there are thousands of lines of legacy code involved, going through
all of them and adding qualifications and/or suitable using declarations
may take more man-hours than management can accept - or may delay the
completion of the project too far past the deadline. Under those
circumstances the only choices may be using directives or not upgrading to
the newer version of the compiler and continuing to use the pre-standard
version of STL.
>It defeats the whole purpose of namespaces. A better approach would be
>something along the lines of:
>
>#ifdef COMPILER_HAS_NAMESPACES
>#define STD std::
>#else
>#define STD
>#endif
>
>STD vector<int> v;
>
>Now you retain the benefit of namespace on compilers that are conformant (and
>I'd recommend doing the main chunk of coding on such a compiler), while
>allowing non-conformant compilers to compile as well.
>
IF you have time in your schedule, and money in your budget, to *do* all that
"non-productive" work of changing the access.
Yes, I would much prefer to do things right, but one doesn't always have
that choice.
---
[ 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: 1999/04/14 Raw View
"Stanley Friesen [Contractor]" wrote:
...
> Maybe I should have said "the only viable approach". When, as in this
> case, there are thousands of lines of legacy code involved, going through
> all of them and adding qualifications and/or suitable using declarations
> may take more man-hours than management can accept - or may delay the
> completion of the project too far past the deadline. Under those
> circumstances the only choices may be using directives or not upgrading to
> the newer version of the compiler and continuing to use the pre-standard
> version of STL.
Go for the latter - if you don't have time to do that, you certainly
don't have time to fix all of the other problems that will be created by
all of the other differences between pre-standard and standard STL.
Standard C++ is (almost) a new language.
---
[ 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: paulp.removethis@ccnet.com (Paul)
Date: 1999/04/08 Raw View
I'm strying to write portable C++ code and am running
into a problem with respect to STL and namespaces. Some
STL implementations allows STL classes (e.g. vector) to
simply be used as is. However, some implementations,
notably brand MS, won't let you use the classes in a cpp
file unless you put 'using namespace std;' at the top
of the cpp file.
What is correct? I'd rather not be told to simply put
'using namespace std' at the top of all cpp files, because
we have *hundreds* of source files that use STL classes.
Paul
---
[ 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: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/04/10 Raw View
On 08 Apr 99 22:23:56 GMT, Paul <paulp.removethis@ccnet.com> wrote:
>I'm strying to write portable C++ code and am running
>into a problem with respect to STL and namespaces. Some
>STL implementations allows STL classes (e.g. vector) to
>simply be used as is. However, some implementations,
>notably brand MS, won't let you use the classes in a cpp
>file unless you put 'using namespace std;' at the top
>of the cpp file.
>
>What is correct? I'd rather not be told to simply put
>'using namespace std' at the top of all cpp files, because
>we have *hundreds* of source files that use STL classes.
If you say
#include <vector>
then you must say
std::vector<int> v;
Try to use this method if possible as you get the full
benefit of namespaces.
If you say
#include <vector.h>
then you must say
vector<int> v;
Try not to use this method if possible as it zeroes out
the benefits of namespaces. You might need to use this
method as a transition tool. Say you have lots of old
C++ code that doesn't use namespaces. So then you'd
write "#include <vector.h>". But later on, when you
have more time, you'd change this to "#include <vector>"
and change usage of "vector<int>" to "std::vector<int>".
A future revision of C++ might throw out the header
files with the .h suffix.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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: "John Lund" <jlund@allaire.com>
Date: 1999/04/10 Raw View
Paul wrote in message <370e0ec5.3665495@news.ccnet.com>...
>Some STL implementations allows STL classes (e.g. vector) to
>simply be used as is. However, some implementations,
>notably brand MS, won't let you use the classes in a cpp
>file unless you put 'using namespace std;' at the top
>of the cpp file.
MS is correct here. Presumably your other vendors will be updating their
support for namespaces. Refer to Stroustrup, 3rd Ed. under namespaces or the
intro to the standard library.
Although your compiler requirements don't allow you to use qualified names
like "std::vector" you can reduce your global namespace pollution with
something like this:
#include <memory>
#ifdef NAMESPACES_SUPPORTED
using std::auto_ptr
#endif
I suppose you a macro would allow you to localize this even more:
#if defined(NAMESPACES_UNSUPPORTED)
#define USING(name)
#else
#define USING(name) using name;
#endif
#include <memory>
void foo()
{
USING(std::auto_ptr);
auto_ptr<int> p;
}
I realize that neither of these solutions save you from a lot of file
modifications. I suppose the way to minimize that is to include a common
header just about everywhere. Bummer.
-john, jlund@allaire.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: "Joerg Schaible" <Joerg.Schaible.@.gft.de@proxy.st-georgen.gft.de>
Date: 1999/04/11 Raw View
Paul wrote in message <370e0ec5.3665495@news.ccnet.com>...
>I'm strying to write portable C++ code and am running
>into a problem with respect to STL and namespaces. Some
>STL implementations allows STL classes (e.g. vector) to
>simply be used as is. However, some implementations,
>notably brand MS, won't let you use the classes in a cpp
>file unless you put 'using namespace std;' at the top
>of the cpp file.
>
>What is correct? I'd rather not be told to simply put
>'using namespace std' at the top of all cpp files, because
>we have *hundreds* of source files that use STL classes.
According to the standard all classes and functions of the RTL (and STL) are
in the namespace std. So to write "portable C++ code" (i.e portable ANSI C++
3.0 code) you *have to* add the using namespace std ...
Greetings, J rg
---
BTW: It is normally better to answer to the group! For direct mail reply
exchange the ".AT."
---
[ 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: stanley@west.sun.com (Stanley Friesen [Contractor])
Date: 1999/04/11 Raw View
In article <370e0ec5.3665495@news.ccnet.com>,
Paul <paulp.removethis@ccnet.com> wrote:
>I'm strying to write portable C++ code and am running
>into a problem with respect to STL and namespaces. Some
>STL implementations allows STL classes (e.g. vector) to
>simply be used as is. However, some implementations,
>notably brand MS, won't let you use the classes in a cpp
>file unless you put 'using namespace std;' at the top
>of the cpp file.
OR you qualify the classes with the namespace name 'std'.
For example, you may write:
std::vector<MyClass> mcv;
OR if you specify something like "using std::vector;", then you
may use 'vector' without qualification to refer to std::vector.
One of these is the recommended way to go, except for porting legacy code.
>
>What is correct?
The version of STL in the Standard is specified to be in namespace std.
> I'd rather not be told to simply put
>'using namespace std' at the top of all cpp files, because
>we have *hundreds* of source files that use STL classes.
Sounds like legacy code.
For that, "using namespace std;" is probably the best approach. One useful
shortcut is to put this in a private header file that also includes the
standard header(s), and then use that wrapper instead of the standard header
in all your source files.
---
[ 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 ]