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]

Template problem: Is it me, or is it the compiler?


I posted this in late December, but it may have been missed over the
holidays, as I did not hear anything much. I have subsequently
tried it with a later snapshot of gcc, and have got the same results.
I had one email confirming my recollection that with oilder versions
of egcs the error did not occur. I am pretty convinced that my code
is quite correct, so I am sure that it is a bug in gcc-2.95.

Compiling the following code with gcc-2.95.2 gives the error:

test1.cc: In function `int main()':
test1.cc:32: no match for `ostream & << R<int,-5,5> &'
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/iostream.h:77:
candidates are: class ostream & ostream::operator <<(char)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/iostream.h:78:                
class ostream & ostream::operator <<(unsigned char)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/iostream.h:79:                
class ostream & ostream::operator <<(signed char)

etc. etc. etc.

The code is:

#include <iostream>

template<class T, T lower, T upper>
class R
{
  public:
    R() { value = lower; }
    T val() const { return value; }
  private:
    T value;
};

template<class T, T lower, T upper>
ostream&
operator<<(ostream& out_stream, const R<T, lower, upper>& the_value)
{
  return out_stream << the_value.val();
} 

int main()
{
  R<int, -5, 5> f;
  cout << endl;
  cout << "Number is: " << f << endl;
  return 0;
}

If the class is defined slightly differently, and all references
to it are altered accordingly, all is OK:

template<class T, T lower, T upper> is replaced with:
template<class T, int lower, int upper>

Of course, this will be no good in a real example, as the class
needs to be able to specify the other template parameters in
terms of T and not in terms of int!

I seem to recall that is worked with some earlier version of g++

Michael Ben-Gershon
mybg@netvision.net.il

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