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

Michael Ben-Gershon mybg@netvision.net.il
Thu Dec 30 08:24:00 GMT 1999


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


More information about the Gcc-bugs mailing list