This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
'using namespace std' before including header
- From: gkeating at apple dot com (Geoffrey Keating)
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 18 Oct 2006 13:08:40 -0700 (PDT)
- Subject: 'using namespace std' before including header
As far as I can tell from the C++ standard, there is no implicit
predeclaration of the 'std' namespace. Thus, a file that contains
using namespace std;
requires an error, due to [basic.lookup.unqual] paragraph 1, "If no
declaration is found, the program is ill-formed" or possibly
[lib.using.headers], "A translation unit ... shall include the header
lexically before the first reference to any of the entities it
declares or first defines in that translation unit."
As far as I can tell, eb133c.C is conforming. The standard says "Each
name declared as an object with external linkage in a header is
reserved" and "Each name from the Standard C library declared with
external linkage is reserved to the implementation for use as a name
with extern "C" linkage", but namespaces are not objects and cannot
have extern "C" linkage. I have no idea whether this is intended, I
can think of good arguments for and against.
eb133.C has become an exact copy of eb133b.C, so I removed it.
A side-effect of the IMA work is that this will be fixed.
Tested with 'make -C gcc check-g++ RUNTESTFLAGS=old-deja.exp=eb133\*'.
--
- Geoffrey Keating <geoffk@apple.com>
===File ~/patches/gcc-cp-stdbeforedecl.patch================
2006-10-18 Geoffrey Keating <geoffk@apple.com>
* g++.old-deja/g++.robertl/eb133b.C: Add XFAILed error for
'using namespace std' without previous declaration.
* g++.old-deja/g++.robertl/eb133.C: Remove, exact dup of eb133b.C.
* g++.old-deja/g++.robertl/eb133c.C: New.
Index: g++.old-deja/g++.robertl/eb133b.C
===================================================================
--- g++.old-deja/g++.robertl/eb133b.C (revision 117786)
+++ g++.old-deja/g++.robertl/eb133b.C (working copy)
@@ -3,7 +3,7 @@
// From: Klaus-Georg Adams <Klaus-Georg.Adams@chemie.uni-karlsruhe.de>
// Reported against EGCS snaps 98/06/28.
-using namespace std;
+using namespace std; // { dg-error "" "" { xfail *-*-* } }
int main()
{
@@ -14,5 +14,3 @@
}
return 0;
}
-
-
Index: g++.old-deja/g++.robertl/eb133c.C
===================================================================
--- g++.old-deja/g++.robertl/eb133c.C (revision 0)
+++ g++.old-deja/g++.robertl/eb133c.C (revision 0)
@@ -0,0 +1,17 @@
+// { dg-do assemble }
+// Gives ICE 109
+// From: Klaus-Georg Adams <Klaus-Georg.Adams@chemie.uni-karlsruhe.de>
+// Reported against EGCS snaps 98/06/28.
+
+namespace std { }
+using namespace std;
+
+int main()
+{
+ try {
+ }
+ catch (bad_alloc) { // { dg-error "" }
+ return 1;
+ }
+ return 0;
+}
Index: g++.old-deja/g++.robertl/eb133.C
===================================================================
--- g++.old-deja/g++.robertl/eb133.C (revision 117786)
+++ g++.old-deja/g++.robertl/eb133.C (working copy)
@@ -1,17 +0,0 @@
-// { dg-do assemble }
-// Gives ICE 109
-// From: Klaus-Georg Adams <Klaus-Georg.Adams@chemie.uni-karlsruhe.de>
-// Reported against EGCS snaps 98/06/28.
-
-using namespace std;
-
-int main()
-{
- try {
- }
- catch (bad_alloc) { // { dg-error "" }
- return 1;
- }
- return 0;
-}
-
============================================================