Bug 32358 - Compiler chokes on C++ template friend functions
Compiler chokes on C++ template friend functions
Status: CLOSED NOTABUG
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
7.0
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
David Lawrence
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2001-03-19 22:04 EST by Randy Cushman
Modified: 2007-04-18 12:32 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2001-03-19 22:09:42 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
Sample source file (2.31 KB, text/plain)
2001-03-19 22:05 EST, Randy Cushman
no flags Details
sample source file (860 bytes, text/plain)
2001-03-19 22:06 EST, Randy Cushman
no flags Details

  None (edit)
Description Randy Cushman 2001-03-19 22:04:03 EST
From Bugzilla Helper:
User-Agent: Mozilla/4.75 [en] (X11; U; Linux 2.2.16-22 i686)




Reproducible: Always
Steps to Reproduce:
1.gcc matrix-gccbug.cxx
2.
3.
	

Actual Results:  
In file included from matrix-gccbug.cxx:2:
matrix.hxx:39: warning: friend declaration `matrix<T> operator+ (const
matrix<T> &, const matrix<T> &)'
matrix.hxx:39: warning:   declares a non-template function
matrix.hxx:39: warning:   (if this is not what you intended, make sure
the function template has already been declared and add <> after the
function name here) -Wno-non-template-friend disables this warning.
matrix.hxx:40: warning: friend declaration `ostream &operator<<
(ostream &, const matrix<T> &)'
matrix.hxx:40: warning:   declares a non-template function
/tmp/ccpqC8WI.o: In function `main':
/tmp/ccpqC8WI.o(.text+0x68): undefined reference to `operator+(matrix<int>
const &, matrix<int> const &)'
/tmp/ccpqC8WI.o(.text+0x77): undefined reference to `cout'
/tmp/ccpqC8WI.o(.text+0x7c): undefined reference to `operator<<(ostream &,
matrix<int> const &)'
/tmp/ccpqC8WI.o(.text+0x8b): undefined reference to `cout'
/tmp/ccpqC8WI.o(.text+0x90): undefined reference to `operator<<(ostream &,
matrix<int> const &)'
/tmp/ccpqC8WI.o(.text+0x9f): undefined reference to `cout'
/tmp/ccpqC8WI.o(.text+0xa4): undefined reference to `operator<<(ostream &,
matrix<int> const &)'
collect2: ld returned 1 exit status

Expected Results:  Clean compile.  Run displays two matrices added to make
thrid matrix.

Expected results received from Compaq C++ for OpenVMS Alpha V 6.2.
Expected results received from IBM C/C++ Tools V 2.01 for OS/2.
Comment 1 Randy Cushman 2001-03-19 22:05:42 EST
Created attachment 13083 [details]
Sample source file
Comment 2 Randy Cushman 2001-03-19 22:06:31 EST
Created attachment 13084 [details]
sample source file
Comment 3 Randy Cushman 2001-03-19 22:09:31 EST
Have reproduced with gcc-2.96-60 and gcc-2.96-69.
Comment 4 Jakub Jelinek 2001-03-20 11:46:20 EST
Why do you think this is a bug? The source does not follow ISO C++ spec
and g++ even tells you step by step what you probably want to do and how.
If you add:
template<class T>
class matrix;

template<class T>
matrix<T> operator+( const matrix<T>&, const matrix<T>& );

template<class T>
ostream& operator<<( ostream& stream, const matrix<T>& value );

above template<class T> class matrix declaration, it compiles just fine
and works. The fact that the 2 above mentioned compilers accepted it does not
mean it is correct C++.
Comment 5 Randy Cushman 2001-03-21 09:26:16 EST
Thanks for the edification.  I was thrown off by errors received when I
attempted to implement the changes detailed in the above diagnostic messages.
These errors were caused by using gcc instead of g++.  It didn't help that
none of the C++ books I referenced had an example of a template class friend
function.

Note You need to log in before you can comment on or make changes to this bug.