public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/41020]  New: Can't declare an extern "C" friend.
@ 2009-08-09 21:30 davek at gcc dot gnu dot org
  2009-08-09 21:32 ` [Bug c++/41020] " davek at gcc dot gnu dot org
                   ` (16 more replies)
  0 siblings, 17 replies; 19+ messages in thread
From: davek at gcc dot gnu dot org @ 2009-08-09 21:30 UTC (permalink / raw)
  To: gcc-bugs

Attempting to declare a previously-declared extern "C" function as a friend
within a class definition fails with an error that the friend declaration
ambiguates the original extern "C" declaration.  It appears that the friend
declaration is taken as an overload with "C++" linkage, since G++ then
complains that the class members are private within the context of the extern
"C" function definition.

$ cat friend.cxx

extern "C"
{
  int fork (void);
};

class frok
{
  int this_errno;
  friend int fork (void);
};

extern "C" int
fork (void)
{
  frok grouped;
  return grouped.this_errno;
}

$ g++-4 -c friend.cxx -o friend.o
friend.cxx:10:24: error: new declaration 'int fork()'
friend.cxx:4:7: error: ambiguates old declaration 'int fork()'
friend.cxx: In function 'int fork()':
friend.cxx:9:7: error: 'int frok::this_errno' is private
friend.cxx:17:18: error: within this context

$

  According to, for example, "C++ in a nutshell"
(http://books.google.co.uk/books?id=91JTA9B_m44C&pg=PA170&lpg=PA170&dq=friend+%22storage+class%22&source=bl&ots=HrN4X1Y5wu&sig=N9rnB8r_YnxbH2hiWGqtbAWbWyk&hl=en&ei=oDt_SqmvGJKNjAe9k93wAQ&sa=X&oi=book_result&ct=result&resnum=5#v=onepage&q=friend%20%22storage%20class%22&f=false),
a function in a friend declaration should "retain its original linkage" when it
has already been declared.  That is referring to the case of applying a storage
class specifier (static) to the prior declaration that isn't syntactically
valid within a friend declaration, but it seems like it should apply here too;
the linkage should still be retained, even though I'm not changing the
storage-class specifier.

  There may be some reason why this isn't valid C++, but I think it's probably
a weakness in the parsing of friend declarations.

$ g++-4 -v
Using built-in specs.
Target: i686-pc-cygwin
Configured with: /gnu/gcc/gcc-patched/configure --prefix=/opt/gcc-tools -v
--wit
h-gmp=/usr --with-mpfr=/usr --enable-bootstrap
--enable-version-specific-runtime
-libs --enable-static --enable-shared --enable-shared-libgcc
--disable-__cxa_ate
xit --with-gnu-ld --with-gnu-as --with-dwarf2 --disable-sjlj-exceptions
--disabl
e-symvers --disable-libjava --disable-interpreter --program-suffix=-4
--disable-
libgomp --enable-libssp --enable-libada --enable-threads=posix --with-arch=i686
--with-tune=generic CC=gcc-4 CXX=g++-4 CC_FOR_TARGET=gcc-4 CXX_FOR_TARGET=g++-4
--with-ecj-jar=/usr/share/java/ecj.jar LD=/opt/gcc-tools/bin/ld.exe
LD_FOR_TARGE
T=/opt/gcc-tools/bin/ld.exe AS=/opt/gcc-tools/bin/as.exe
AS_FOR_TARGET=/opt/gcc-
tools/bin/as.exe --disable-win32-registry --disable-libgcj-debug
--enable-langua
ges=c,c++,ada
Thread model: posix
gcc version 4.5.0 20090730 (experimental) (GCC)


-- 
           Summary: Can't declare an extern "C" friend.
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: davek at gcc dot gnu dot org
 GCC build triplet: i686-pc-cygwin
  GCC host triplet: i686-pc-cygwin
GCC target triplet: i686-pc-cygwin


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41020


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2009-10-30 16:07 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-09 21:30 [Bug c++/41020] New: Can't declare an extern "C" friend davek at gcc dot gnu dot org
2009-08-09 21:32 ` [Bug c++/41020] " davek at gcc dot gnu dot org
2009-08-10 10:40 ` jwakely dot gcc at gmail dot com
2009-08-10 12:45 ` [Bug c++/41020] [4.5 Regression] " rguenth at gcc dot gnu dot org
2009-08-10 16:17 ` davek at gcc dot gnu dot org
2009-08-10 17:05 ` jwakely dot gcc at gmail dot com
2009-08-10 17:16 ` davek at gcc dot gnu dot org
2009-08-24  1:00 ` mmitchel at gcc dot gnu dot org
2009-09-18  3:56 ` pinskia at gcc dot gnu dot org
2009-10-04 19:14 ` [Bug c++/41020] [4.5 Regression] Can't declare an extern "C" friend of a builtin function pinskia at gcc dot gnu dot org
2009-10-23 18:19   ` Dodji Seketeli
2009-10-23 18:20 ` dodji at redhat dot com
2009-10-23 18:34 ` dodji at gcc dot gnu dot org
2009-10-23 19:32 ` dodji at redhat dot com
2009-10-26 14:40 ` dodji at gcc dot gnu dot org
2009-10-26 16:06 ` dodji at gcc dot gnu dot org
2009-10-27 10:29 ` jakub at gcc dot gnu dot org
2009-10-29 16:53 ` law at gcc dot gnu dot org
2009-10-30 16:07 ` hjl at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).