This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/24449] Unable to declare friend main() from class template


Hi

I believe that this patch fixes PR24449. The problem was that it bailed
out for every declaration of ::main that had something in common with
templates, so I added more precise checking for declaration context.

Patch also contains a testcase that includes every imaginable main
declaration.

Bootstrap passed, check passed without new failures, both on
i686-pc-linux-gnu.

Petr Machata

2005-21-11  Petr Machata  <machata@post.cz>

	PR Bug c++/24449
	* decl.c (grokfndecl): Check if it's really template ::main
	that we are declaring. Mains inside classes and namespaces
	are ok, templates or not.

Index: gcc/testsuite/g++.dg/template/friend40.C
===================================================================
--- gcc/testsuite/g++.dg/template/friend40.C	(revision 0)
+++ gcc/testsuite/g++.dg/template/friend40.C	(revision 0)
@@ -0,0 +1,71 @@
+// { dg-do compile }
+
+// Copyright (C) 2005 Free Software Foundation, Inc.
+
+// PR c++/24449 Unable to declare friend main() from class template
+// friend main declared in template class isn't template main
+
+class Fooa {
+  friend int main();
+  template<class> int main();
+};
+
+template <class T> class Foob {
+  friend int main();
+  int i;
+  template<class> int main();
+};
+
+namespace SomeNamespace
+{
+  struct OtherClass {
+    template<class> friend int main();
+    private: int i;
+  };
+
+  template <class T>
+  struct YetOtherClass {
+    template<class> friend int main();
+  };
+
+  template <class T>
+  struct DifferentClass {
+    int main();
+  };
+
+  template <class T>
+  struct CompletelyDifferentClass {
+    template<class> int main();
+  };
+
+  template<class U> int main() {
+    OtherClass a;
+    a.i = 7;
+  }
+}
+
+int main()
+{
+    Foob<void> a;
+    a.i = 7;
+    SomeNamespace::main<void>();
+}
+
+
+
+class Fooc {
+	template<class T> friend int main(); /* { dg-error "cannot declare .::main. to be a template" } */
+};
+
+template<class T> class Food {
+	template<class U> friend int main(); /* { dg-error "cannot declare .::main. to be a template" } */
+};
+
+namespace SomeNamespace
+{
+	struct SomeClass {
+		template<class> friend int ::main(); /* { dg-error "cannot declare .::main. to be a template" } */
+	};
+}
+
+template<class U> int main() {} /* { dg-error "cannot declare .::main. to be a template" } */
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c	(revision 106694)
+++ gcc/cp/decl.c	(working copy)
@@ -5720,7 +5720,9 @@
 
   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
     {
-      if (processing_template_decl)
+      /* main must not be template only if it's actually ::main */
+      if (innermost_scope_kind() == sk_template_parms
+	  && !namespace_bindings_p())
 	error ("cannot declare %<::main%> to be a template");
       if (inlinep)
 	error ("cannot declare %<::main%> to be inline");

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]