C++ bug 764
Nathan Sidwell
nathan@codesourcery.com
Fri May 4 04:23:00 GMT 2001
Hi,
In reexamining bug 764, I'm not convinced the code is well-formed.
(Martin, we may have corresponded on this, but I cannot find any of
that email).
The code in question is this [note, that this is in a follow up, as the
original bug report code wasn't what martin intended]
template <class T>
struct S
{
friend bool operator== (const S&, const S&) {
return true;
}
};
int main ()
{
// S<int> s;
const S<int> *p = 0;
*p == *p; // error
}
g++ says,
test.cpp: In function `int main()':
test.cpp:33: no match for `const S<int> & == const S<int> &'
S<T>::operator== declares an ordinary non-template function. See 14.5.3/1
last bullet, and in the example is is 'process'. Each specialization of
S<T> gets an appropriate non-template operator== as a friend. Does the
declaration of that function exist before the instantiation of the
corresponding S<T>? As you'll see from the above code, nothing before
the *p == *p requires the instantiation of S<int>. Uncommenting the
declaration of s above will instantiate S<int> and remove the error.
11.4/5 gives the non-template case, and the friend function declaration
exists (in the enclosing namespace scope), at the end of the class
definition.
14.7.1 is also relevant and says that instantiation happens 'when the
completeness of the class type affects the semantics of the program'.
Well, it looks like it does here, but that sure seems weird. We'd have
to instantiate templates which declared such friend functions earlier
than we do.
nathan
--
Dr Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
More information about the Gcc-bugs
mailing list