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

Luis Miguel Moreira miguel@idiap.ch
Fri Aug 20 07:57:00 GMT 1999


(I'm sorry, I tried to put end-of-the-line numbers on the code of my
previous message but I got it all scrambled. I am re-sending it.)

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

#define tcT template <class T>

// forward declarations
tcT class number;
tcT number<T> operator + (const T,  const number<T>);

// class definition
tcT class number {
      public:

         number() : value(0) {};

         number<T> operator + (const T v) const;
         friend number<T> operator + <> (const T v,
                                         const number<T> num);
         T value;
};

// class member operator
tcT number<T> number<T>::operator + (const T v) const
{
      number<T> newNum;
      newNum.value = this->value + v;
      return newNum;
}

// friend operator
tcT number<T> operator + (const T v,  const number<T> num)
{
      number<T> newNum;
      newNum.value = num.value + v;
      return newNum;
}

int main()
{
      number<int> n;  printf( "%d\n", n.value );
      n = 2 + n;      printf( "%d\n", n.value );
      return 0;
}
------------------------------------------------------------------

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




More information about the Gcc-bugs mailing list