This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Template problem: Is it me, or is it the compiler?
- To: gnu-gcc-bug at moderators dot isc dot org
- Subject: Template problem: Is it me, or is it the compiler?
- From: Michael Ben-Gershon <mybg at netvision dot net dot il>
- Date: Mon, 10 Jan 2000 11:29:40 +0200
- Newsgroups: gnu.gcc.bug
- Organization: My Office
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