Bug 24449 - Unable to declare friend main() from class template
Summary: Unable to declare friend main() from class template
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.2
: P2 minor
Target Milestone: 4.8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2005-10-19 23:24 UTC by Petr Machata
Modified: 2012-10-12 14:39 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.0
Known to fail: 2.95.3, 3.0.4, 4.0.0
Last reconfirmed: 2009-04-16 18:05:11


Attachments
Patch against gcc/cp/decl.c, and a testcase (457 bytes, patch)
2005-11-10 14:21 UTC, Petr Machata
Details | Diff
Check if we are really declaring template ::main. (716 bytes, patch)
2005-11-16 15:25 UTC, Petr Machata
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Petr Machata 2005-10-19 23:24:25 UTC
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'
Comment 1 Andrew Pinski 2005-10-19 23:32:44 UTC
Confirmed, not a regression.
Comment 2 Petr Machata 2005-11-10 14:21:47 UTC
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.
Comment 3 Petr Machata 2005-11-16 15:25:13 UTC
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.
Comment 4 Petr Machata 2005-11-18 14:27:50 UTC
> 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.
Comment 5 Andrew Pinski 2005-11-18 14:59:44 UTC
Can you read http://gcc.gnu.org/contribute.html and send the patch to gcc-patches@gcc.gnu.org?
Comment 6 Paolo Carlini 2012-10-12 10:56:33 UTC
On it.
Comment 7 paolo@gcc.gnu.org 2012-10-12 14:38:19 UTC
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
Comment 8 Paolo Carlini 2012-10-12 14:39:46 UTC
Fixed.