Compiler won't let me declare ::main() a friend from inside template class. It's not a problem in practice (who would want to do such thing?), however I don't see a reason for such a behavior. If I change the name to something else, it works correctly. gcc 3.3.2 and gcc 2.95.3 are also behaving this way. Preprocessed main.cc: # 1 "main.cc" # 1 "<built-in>" # 1 "<command line>" # 1 "main.cc" template< class X > class Foo { private: int i; friend int main(); }; int main() { int i = Foo<int>().i; } Command line: gcc-4.0 -v -save-temps -Wall main.cc Compiler output: Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../gcc-4.0.2/configure --prefix=/opt/gcc-4.0/ --program-suffix=-4.0 --enable-threads Thread model: posix gcc version 4.0.2 /opt/gcc-4.0/bin/../libexec/gcc/i686-pc-linux-gnu/4.0.2/cc1plus -E -quiet -v -iprefix /opt/gcc-4.0/bin/../lib/gcc/i686-pc-linux-gnu/4.0.2/ -D_GNU_SOURCE main.cc -mtune=pentiumpro -Wall -fpch-preprocess -o main.ii ignoring nonexistent directory "/opt/gcc-4.0/bin/../lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../i686-pc-linux-gnu/include" ignoring duplicate directory "/opt/gcc-4.0//lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../include/c++/4.0.2" ignoring duplicate directory "/opt/gcc-4.0//lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../include/c++/4.0.2/i686-pc-linux-gnu" ignoring duplicate directory "/opt/gcc-4.0//lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../include/c++/4.0.2/backward" ignoring duplicate directory "/opt/gcc-4.0//lib/gcc/i686-pc-linux-gnu/4.0.2/include" ignoring nonexistent directory "/opt/gcc-4.0//lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../i686-pc-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /opt/gcc-4.0/bin/../lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../include/c++/4.0.2 /opt/gcc-4.0/bin/../lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../include/c++/4.0.2/i686-pc-linux-gnu /opt/gcc-4.0/bin/../lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../include/c++/4.0.2/backward /opt/gcc-4.0/bin/../lib/gcc/i686-pc-linux-gnu/4.0.2/include /usr/local/include /opt/gcc-4.0//include /usr/include End of search list. /opt/gcc-4.0/bin/../libexec/gcc/i686-pc-linux-gnu/4.0.2/cc1plus -fpreprocessed main.ii -quiet -dumpbase main.cc -mtune=pentiumpro -auxbase main -Wall -version -o main.s GNU C++ version 4.0.2 (i686-pc-linux-gnu) compiled by GNU C version 4.0.2. GGC heuristics: --param ggc-min-expand=81 --param ggc-min-heapsize=96807 main.cc:6: error: cannot declare '::main' to be a template main.cc: In function 'int main()': main.cc:11: warning: unused variable 'i'
Confirmed, not a regression.
Created attachment 10202 [details] Patch against gcc/cp/decl.c, and a testcase This is rather straightforward solution for this PR. It allows parsing of the code like this: template< class > class { friend int ::main(); }; But unfortunately also this: class { template< class > friend int ::main(); }; I'm going to investigate the latter case now, but I don't yet have enough knowledge to propose a solution. IMHO this should fail, but maybe a warning is enough? The patch contains also a testcase. I tried to prepare the patch with all rules in mind, but I could have overlooked something.
Created attachment 10251 [details] Check if we are really declaring template ::main. I believe that this patch does the right thing. I've done bootstrap on i686-pc-linux-gnu, but I won't be able to run testsuite before friday.
> I believe that this patch does the right thing. I've done bootstrap on > i686-pc-linux-gnu, but I won't be able to run testsuite before friday. Which just passed on i686-pc-linux-gnu.
Can you read http://gcc.gnu.org/contribute.html and send the patch to gcc-patches@gcc.gnu.org?
On it.
Author: paolo Date: Fri Oct 12 14:38:11 2012 New Revision: 192402 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=192402 Log: /cp 2012-10-12 Paolo Carlini <paolo.carlini@oracle.com> PR c++/24449 * decl.c (grokfndecl): When checking for ::main declarations use PROCESSING_REAL_TEMPLATE_DECL_P(). /testsuite 2012-10-12 Paolo Carlini <paolo.carlini@oracle.com> PR c++/24449 * g++.dg/parse/friend-main.C: New. Added: trunk/gcc/testsuite/g++.dg/parse/friend-main.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/decl.c trunk/gcc/testsuite/ChangeLog
Fixed.