This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

gcc 2.95(.1) bug: template + friend + overloaded operator



---------------------------------------------
gcc version:    2.95 and 2.95.1
system type:    SunOS 5.7 Generic sun4u
options passed: none
---------------------------------------------


Hello,

the following C++ code:

------------------------------------------------------------------
#include <stdio.h>                                               1
 																					  2
#define tcT template <class T>											  3
 																					  4
// forward declarations														  5
tcT class number;																  6
tcT number<T> operator + (const T,  const number<T>);				  7
 																					  8
// class definition															  9
tcT class number {															 10
      public:																	 11
 																					 12
         number() : value(0) {};											 13
 																					 14
         number<T> operator + (const T v) const;					 15
         friend number<T> operator + <> (const T v,				 16
                                         const number<T> num);  17
         T value;																 18
};																					 19
 																					 20
// class member operator													 21
tcT number<T> number<T>::operator + (const T v) const				 22
{																					 23
      number<T> newNum;														 24
      newNum.value = this->value + v;									 25
      return newNum;															 26
}																					 27
 																					 28
// friend operator															 29
tcT number<T> operator + (const T v,  const number<T> num)		 30
{ 																					 31
      number<T> newNum;														 32
      newNum.value = num.value + v;										 33
      return newNum;															 34
}																					 35
 																					 36
int main()																		 37
{																					 38
      number<int> n;  printf( "%d\n", n.value );					 39
      n = 2 + n;      printf( "%d\n", n.value ); 					 30
      return 0;																 41
}																					 42
------------------------------------------------------------------

produces the following compilation error:

------------------------------------------------------------------
~> gcc testFT.cpp
testFT.cpp: In instantiation of `number<int>':
testFT.cpp:39:   instantiated from here
testFT.cpp:17: invalid use of undefined type `class number<int>'
testFT.cpp:19: forward declaration of `class number<int>'
testFT.cpp:17: confused by earlier errors, bailing out
------------------------------------------------------------------

with both versions 2.95 and 2.95.1 of gcc. It compiles and runs
without problems if:

  - earlier versions of gcc are used, or
  - no templates are used, or
  - only one (either one) of the operator functions is present.

The files testFT.cpp and testFT.ii (gzipped) are attached below.

Thanks,

Miguel.

-- 
I D I A P              C.P. 592, 1920 Martigny, Switzerland
Institut Dalle Molle d'Intelligence Artificielle Perceptive
___________________________________________________________
Miguel Moreira                        tel: +41.27.721 77 45
Research Assistant                    fax:        721 77 12
email: miguel@idiap.ch          http://www.idiap.ch/~miguel


testFT.cpp


testFT.ii


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]