Bug 32358 - Compiler chokes on C++ template friend functions
Summary: Compiler chokes on C++ template friend functions
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
(Show other bugs)
Version: 7.0
Hardware: i386 Linux
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
Depends On:
TreeView+ depends on / blocked
Reported: 2001-03-20 03:04 UTC by Randy Cushman
Modified: 2007-04-18 16:32 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2001-03-20 03:09:42 UTC
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
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-20 03:05 UTC, Randy Cushman
no flags Details
sample source file (860 bytes, text/plain)
2001-03-20 03:06 UTC, Randy Cushman
no flags Details

Description Randy Cushman 2001-03-20 03:04:03 UTC
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

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-20 03:05:42 UTC
Created attachment 13083 [details]
Sample source file

Comment 2 Randy Cushman 2001-03-20 03:06:31 UTC
Created attachment 13084 [details]
sample source file

Comment 3 Randy Cushman 2001-03-20 03:09:31 UTC
Have reproduced with gcc-2.96-60 and gcc-2.96-69.

Comment 4 Jakub Jelinek 2001-03-20 16:46:20 UTC
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 14:26:16 UTC
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

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