public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* Template Friend Weirdness
@ 1999-07-08 11:28 John McCorquodale
  1999-07-10  3:19 ` Martin v. Loewis
  0 siblings, 1 reply; 2+ messages in thread
From: John McCorquodale @ 1999-07-08 11:28 UTC (permalink / raw)
  To: egcs-bugs

Guys,

I'm trying to declare a global template function to be a friend of a template
class.  Problems with this are leading in a mysterious and seemingly broken
way to a template f'n failing to be instantiated.  Here's the code:

---

#include <stdio.h>

template<class T> void TFunc(T& bob){
        printf("TFunc() says bob's i = %d\n",bob.i);
}

template<class T> class SomeClass {
public:
    SomeClass(){i=123456789;};
    ~SomeClass(){};

private:
        T i;

        friend void TFunc(SomeClass<T>&);
};

int main() {
        SomeClass<int> SC;
        TFunc(SC);
        return 0;
}

---

This compiles and runs fine on Microsoft C++, Borland C++ for Windows and
the SGI MIPSpro C++ copmiler (the current 7.30).  In egcs-19990629 on a
i386 Linux-2.2.10 / glibc-2.1.1 machine, it says:

$ g++ -o n n.C
n.C:15: warning: friend declaration `void TFunc(SomeClass<T> &)'
n.C:15: warning:   declares a non-template function
n.C:15: warning:   (if this is not what you intended, make sure
n.C:15: warning:   the function template has already been declared,
n.C:15: warning:   and add <> after the function name here)
n.C:15: warning:   -Wno-non-template-friend disables this warning.
/tmp/ccZPUmUN.o: In function `main':
/tmp/ccZPUmUN.o(.text+0x1d): undefined reference to `TFunc(SomeClass<int> &)'
collect2: ld returned 1 exit status

Adding the <> as the warning suggests produces a working compile in egcs, but
breaks all the other compilers (parse errors).  I'm happy to ignore the
warning or turn it off with the indicated switch.

The actual bug appears to be that the global template f'n never gets
instantiated even though it's quite clearly called from main().

Anyway, that seemed broken enough that I thought I'd mail it in.  :)

-mcq
>From tony@waves.esd.mun.ca Thu Jul 08 11:33:00 1999
From: Tony Kocurko <tony@waves.esd.mun.ca>
To: egcs-bugs@egcs.cygnus.com
Subject: -I not always left-to-right
Date: Thu, 08 Jul 1999 11:33:00 -0000
Message-id: <199907081836.QAA11906@tomig.esd.mun.ca>
X-SW-Source: 1999-07/msg00308.html
Content-length: 3432

Hello,

  I have found what I think is a bug in the GNU compiler, but I could well be
misreading the documentation. A makefile for a minimal example of the `bug'
follows my signature line below.

  The gcc documentation says that "If you use more than one `-I' option, the
directories are scanned in left-to-right order...". However, if one has
two different directories, each with an identically named file, and if
that file is a nested include file in another file, then the multiply
occuring file is taken from the same directory as the encompassing include
file rather than from the directory specified by the order of the `-I' command
line switches. For example, if one has this directory structure:

                  include_1/                       include_2/
                  ----+-----                       ----+-----
                      |                                |
          +-----------+-----------+             +------+----+
          |           |           |             |           |
      ----+---    ----+---    ----+---      ----+---    ----+---
      file_1.h    file_2.h    file_3.h      file_2.h    file_3.h

and if include_1/file_1.h includes "file_3.h", then, regardless of the order
of appearence of -I./include_1 and -I./include_2, it will be include_1/file_3.h
that is included. That is, given a compile command like this:

# gcc -I./include_2 -I./include_1 Bug_test.c -o Bug_test

where Bug_test.c includes "file_1.h", "include_1/file_1.h" will be included
rather than "include_2/file_2.h", although -I./include_2 precedes -I./include_1.

Cheers,
Tony Kocurko
---------------------------------------------------------------------------
Seismological Systems Manager             phone : 1-709-737-8898
Department of Earth Sciences              FAX   : 1-709-737-2589
Alexander Murray Building ER-4063         e-mail: tony@waves.esd.mun.ca
Memorial University of Newfoundland       web   : http://weland.esd.mun.ca
St. John's, Newfoundland
Canada      A1B 3X5
---------------------------------------------------------------------------

#----------------------------- Makefile for bug follows -------------------
all:
	@mkdir include_1 include_2
	@echo \#include \"file_3.h\"                  > include_1/file_1.h
	@echo char \*str1 = \"include_1/file_3.h\"\;  > include_1/file_3.h
	@echo char \*str1 = \"include_2/file_3.h\"\;  > include_2/file_3.h
	@echo char \*str2 = \"include_1/file_2.h\"\;  > include_1/file_2.h
	@echo char \*str2 = \"include_2/file_2.h\"\;  > include_2/file_2.h
	@echo \#include \<stdio.h\>                   > Bug_test.c
	@echo \#include \"file_1.h\"                 >> Bug_test.c
	@echo \#include \"file_2.h\"                 >> Bug_test.c
	@echo ""                                     >> Bug_test.c
	@echo int                                    >> Bug_test.c
	@echo main \(int argc, char \*\*argv\) \{    >> Bug_test.c
	@echo "	printf (\"%s\n\", str1);"           >> Bug_test.c
	@echo "	printf (\"%s\n\", str2);"           >> Bug_test.c
	@echo "	exit (0);"                          >> Bug_test.c
	@echo \}                                    >> Bug_test.c
	@gcc -I`pwd`/include_2 -I`pwd`/include_1 Bug_test.c -o Bug_test
	@echo
	@echo
	@echo
	@echo "Documentation expects  \"include_2/file_3.h\""
	@echo "followed by the string \"include_2/file_2.h\""
	@echo
	@echo
	@echo
	@./Bug_test
	@echo
	@echo
	@echo

clean:
	@/bin/rm -rf include_1 include_2 Bug_test Bug_test.c
>From tony@waves.esd.mun.ca Thu Jul 08 11:41:00 1999
From: Tony Kocurko <tony@waves.esd.mun.ca>
To: egcs-bugs@egcs.cygnus.com
Subject: Re: -I not always left-to-right
Date: Thu, 08 Jul 1999 11:41:00 -0000
Message-id: <199907081844.QAA12060@tomig.esd.mun.ca>
X-SW-Source: 1999-07/msg00309.html
Content-length: 154

Sorry about that. I failed to read the very next entry regarding the `-I-'
command line switch for gcc. Boy, do I feel like a dope!

Cheers,
Tony Kocurko
>From law@cygnus.com Thu Jul 08 11:43:00 1999
From: Jeffrey A Law <law@cygnus.com>
To: Luke Diamand <lgd@virata.com>
Cc: egcs-bugs@egcs.cygnus.com
Subject: Re: egcs 19990629 (arm/aout) static member vars end up in common 
Date: Thu, 08 Jul 1999 11:43:00 -0000
Message-id: <2357.931459056@upchuck.cygnus.com>
References: <37835AB0.3DD0C389@virata.com>
X-SW-Source: 1999-07/msg00310.html
Content-length: 786

  In message < 37835AB0.3DD0C389@virata.com >you write:
  > I'm using gcc-2.95 19990629 (prerelease) and I find that, compared to
  > the last version I used (egcs1.1b) it has started to put some symbols
  > into common (.comm) that it never used to.
  > 
  > The sort of code that provokes this looks like the following:
  > 
  >         class Foo
  >         {
  >                 static int x;
  >         };
  > 
  >         int Foo::x;
  > 
  > 
  > I get the following:
  > 
  >     .comm   __3Foo.x, 4     @ 4
  > 
  > Sadly, my assembler and linker (non-gnu) do not understand .comm.
That sounds like a bug in your gcc configuration files.  The configuration
files should define ASM_OUTPUT_COMMON or ASM_OUTPUT_ALIGNED_COMMON to emit
suitable assembly code for commons.

jeff



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

* Re: Template Friend Weirdness
  1999-07-08 11:28 Template Friend Weirdness John McCorquodale
@ 1999-07-10  3:19 ` Martin v. Loewis
  0 siblings, 0 replies; 2+ messages in thread
From: Martin v. Loewis @ 1999-07-10  3:19 UTC (permalink / raw)
  To: mcq; +Cc: egcs-bugs

> In egcs-19990629 on a i386 Linux-2.2.10 / glibc-2.1.1 machine, it
> says:
[...]
> n.C:15: warning: friend declaration `void TFunc(SomeClass<T> &)'
> n.C:15: warning:   declares a non-template function
> n.C:15: warning:   (if this is not what you intended, make sure
> n.C:15: warning:   the function template has already been declared,
> n.C:15: warning:   and add <> after the function name here)
> n.C:15: warning:   -Wno-non-template-friend disables this warning.
[...]
> Adding the <> as the warning suggests produces a working compile in egcs, but
> breaks all the other compilers (parse errors).  I'm happy to ignore the
> warning or turn it off with the indicated switch.

The warning is there for a reason. If you leave your original code, it
expects that there is a new global friend function for each
instantiation.

> The actual bug appears to be that the global template f'n never gets
> instantiated even though it's quite clearly called from main().

This is not a bug. If you leave it as it was, you have to define a
function

void TFunc(SomeClass<int>&);

which is not a template instantiation.

> This compiles and runs fine on Microsoft C++, Borland C++ for
> Windows and the SGI MIPSpro C++ copmiler (the current 7.30).

Then those compilers are all in error. If you want to make a bug
report, you can refer them to section 14.5.3, [temp.friend]/1 of the
C++ standard. Perhaps you could try whether they accept

friend void TFunc<T>(SomeClass<T>&);

first, which is the example explicitly given in the standard.

Regards,
Martin
>From martin@mira.isdn.cs.tu-berlin.de Sat Jul 10 03:28:00 1999
From: "Martin v. Loewis" <martin@mira.isdn.cs.tu-berlin.de>
To: bryanf@simutronics.com
Cc: egcs-bugs@egcs.cygnus.com
Subject: Re: egcs crash with undefined std::set
Date: Sat, 10 Jul 1999 03:28:00 -0000
Message-id: <199907101022.MAA00632@mira.isdn.cs.tu-berlin.de>
References: <000201bec89c$100ea080$2fcc53c6@bryanf.simutronics.com>
X-SW-Source: 1999-07/msg00395.html
Content-length: 200

> The following line of C++ code compiled with egcs-2.91.66 on Intel-Linux
> produces "Internal compiler error 980711."

Thanks for your bug report. gcc-2.95 19990701 fixes this bug.

Regards,
Martin
>From martin@mira.isdn.cs.tu-berlin.de Sat Jul 10 03:28:00 1999
From: "Martin v. Loewis" <martin@mira.isdn.cs.tu-berlin.de>
To: markd@Grizzly.COM
Cc: egcs-bugs@egcs.cygnus.com
Subject: Re: g++ Internal compiler error
Date: Sat, 10 Jul 1999 03:28:00 -0000
Message-id: <199907101026.MAA00644@mira.isdn.cs.tu-berlin.de>
References: <14212.10362.46247.49290@osprey.Grizzly.Com>
X-SW-Source: 1999-07/msg00394.html
Content-length: 547

> gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)

Thanks for your bug report. gcc-2.95 19990701 reports loads of errors,
starting with

In file included from ScoreMatrix.h:20,
                 from ../../build/HeaderCheck.cc:10:
../../src/dp-analyzer/ScoreMatrixTmpl.h: In method `ScoreMatrixTmpl<COLPROBARRAY>::ScoreMatrixTmpl(int, int)':
../../src/dp-analyzer/ScoreMatrixTmpl.h:41: class `ScoreMatrixTmpl<COLPROBARRAY>' does not have any field named `NamedElementProbMatrix'

so it could be that the bug has been fixed.

Regards,
Martin
>From martin@mira.isdn.cs.tu-berlin.de Sat Jul 10 03:33:00 1999
From: "Martin v. Loewis" <martin@mira.isdn.cs.tu-berlin.de>
To: hymie@jyacc.com
Cc: egcs-bugs@egcs.cygnus.com
Subject: Re: g++ allows private types to be used in declarations
Date: Sat, 10 Jul 1999 03:33:00 -0000
Message-id: <199907101027.MAA00654@mira.isdn.cs.tu-berlin.de>
References: <199907071950.PAA13500@calumny.jyacc.com>
X-SW-Source: 1999-07/msg00396.html
Content-length: 158

> This should not be legal, since e is private to x.

Thanks for your bug report. This is a known bug; see 
http://egcs.cygnus.com/bugs.html

Regards,
Martin
>From martin@mira.isdn.cs.tu-berlin.de Sat Jul 10 04:18:00 1999
From: "Martin v. Loewis" <martin@mira.isdn.cs.tu-berlin.de>
To: stefan@vogtner.de
Cc: egcs-bugs@egcs.cygnus.com
Subject: Re: -pedantic causes parse error (template handling)
Date: Sat, 10 Jul 1999 04:18:00 -0000
Message-id: <199907101113.NAA00780@mira.isdn.cs.tu-berlin.de>
References: <37871316.1E66E615@vogtner.de>
X-SW-Source: 1999-07/msg00397.html
Content-length: 226

> 	vector<T>::const_iterator p;
[...]
> bug-19990710.cc:9: parse error before `;'

Thanks for your bug report. This is not a bug; you have to write 

   typename vector<T>::const_iterator p;

in standard C++.

Regards,
Martin
>From vbar@comp.cz Sat Jul 10 05:11:00 1999
From: Vaclav Barta <vbar@comp.cz>
To: egcs-bugs@egcs.cygnus.com
Subject: can't pass pointers to template functions as arguments
Date: Sat, 10 Jul 1999 05:11:00 -0000
Message-id: <3787295B.E2912804@comp.cz>
X-SW-Source: 1999-07/msg00398.html
Content-length: 2427

Hi,

I want to templatize a conversion from exception-throwing function
to error-returning function. But, when I try to instantiate my
function template by passing the function name as a parameter of
another function, egcs crashes. I'm not quite sure what I'm trying
to do is legal, but even if it isn't, the compiler should IMHO say
so.

=== preprocessed source (safecall.ii) ===
# 1 "safecall.cc"
template <typename TReturn,
    TReturn (*ThrowingFunction)(),
    TReturn ErrorCode = TReturn()>
TReturn SafeCall()
{
    try
    {   return ThrowingFunction();
    }
    catch (...)
    {   return ErrorCode;
    }
}

int Callback()
{
    return 1;
}

int LibraryFunction(int (*p)())
{
    return p();
}

int main()
{
    return LibraryFunction(SafeCall<int, Callback>);
}




=== compilation output ===
gcc -v --save-temps safecall.cc
Reading specs from
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.66/cpp -lang-c++ -v
-undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -D__GNUC_MINOR__=91
-D__ELF__ -Dunix -Di386 -D__i386__ -Dlinux -D__ELF__ -D__unix__
-D__i386__ -D__i386__ -D__linux__ -D__unix -D__i386 -D__linux
-Asystem(posix) -D__EXCEPTIONS -Asystem(unix) -Acpu(i386)
-Amachine(i386) -Di386 -D__i386 -D__i386__ -Di686 -Dpentiumpro -D__i686
-D__i686__ -D__pentiumpro -D__pentiumpro__ safecall.cc safecall.ii
GNU CPP version egcs-2.91.66 19990314 (egcs-1.1.2 release) (i386
Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include/g++
 /usr/local/include
 /usr/local/i686-pc-linux-gnu/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.66/include
 /usr/include
End of search list.
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.66/cc1plus
safecall.ii -quiet -dumpbase safecall.cc -version -o safecall.s
GNU C++ version egcs-2.91.66 19990314 (egcs-1.1.2 release)
(i686-pc-linux-gnu) compiled by GNU C version egcs-2.91.66 19990314
(egcs-1.1.2 release).
safecall.cc: In function `int main()':
safecall.cc:26: Internal compiler error 980715.
safecall.cc:26: Please submit a full bug report to
`egcs-bugs@egcs.cygnus.com'.
safecall.cc:26: See <URL: http://egcs.cygnus.com/faq.html#bugreport > for
details.




P.S. I don't read the list, so I appreciate responses via e-mail.

	Bye
		Vasek
--
I have a web spider, too!
http://www.locus.cz/linkcheck/
>From morell@cs.atu.edu Sat Jul 10 08:40:00 1999
From: morell@cs.atu.edu
To: egcs-bugs@egcs.cygnus.com
Subject: Internal compiler error
Date: Sat, 10 Jul 1999 08:40:00 -0000
Message-id: <199907101041.FAA28984@cs.atu.edu>
X-SW-Source: 1999-07/msg00399.html
Content-length: 1249

I was compiling postgres-6.5 when I got the following message:

.
.  (lots of gmake compilation ...)
.

gmake -C libpq++ all
gmake[2]: Entering directory `/usr/src/pgsql/src/interfaces/libpq++'
c++  -I../../backend -I../../include -I../../interfaces/libpq -I../../include -I../../backend   -O2 -Wall -Wmissing-prototypes -fpic   -c pgconnection.cc -o pgconnection.oIn file included from /usr/include/g++/iostream.h:31,
                 from /usr/include/g++/stl_alloc.h:45,
                 from /usr/include/g++/alloc.h:21,
                 from /usr/include/g++/std/bastring.h:39,
                 from /usr/include/g++/string:6,
                 from pgconnection.h:24,
                 from pgconnection.cc:20:
/usr/include/g++/streambuf.h:420: Internal compiler error.
/usr/include/g++/streambuf.h:420: Please submit a full bug report to `egcs-bugs@cygnus.com'.


I then went back and started the make over and this time nothing went
went and the compilation was successful this time with no complaints.
Thought I'd let you know anyway.

Here is the info on the compiler version:

[postgres@apple src]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/specs
gcc version 2.7.2.3

I am on  a RedHat 5.2 system.

Larry Morell


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

end of thread, other threads:[~1999-07-10  3:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-07-08 11:28 Template Friend Weirdness John McCorquodale
1999-07-10  3:19 ` Martin v. Loewis

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).