Re: RAnother friend template question
On 28 Okt., 02:56, "Robert J. Simpson" <rob...@simpson190.fsnet.co.uk>
wrote:
Having recently updated a lot of code to make it compatible with the
standard, I'm left with one case I can't seem to make work. gcc 3.4.4.
reports "no match for operator==" in main().
I've tried all the possible combinations of template/non template
functions
I can think of, adding the '::' prefix to the names etc. but I can't find
a
combination that works. As a quick fix I have got it to work by defining
the
'==' operator inside class Local but I don't really want to do that
because
it makes the source a lot less readable.
Rob.
template <class T> class Test;
template <class T> bool operator == (const typename Test <T>::Local&,
const
typename Test <T>::Local&);
There is a fundamental problem with this signature, namely that
template parameter T is in a non-deducible context. Since you
probably don't want users to use your operator == via explicit
template-argument provision only, like in
bool b = operator==<int>(t1, t2);
I don't see any reasonable approach without a defining friend
function in Test::Local. Your code below does only have a very
simple definition, but should this be more complex, you could
still delegate to a member function or another free friend function
which is called inside the friend definition in Local via explicit
template parameter provision. Here a simple approach based
on a member static function:
template <class T> class Test
{
public:
class Local
{
private:
int x;
Add here:
static bool equals(const Local&, const Local&);
public:
friend bool operator == <T> (const typename ::Test <T>::Local&, const
typename ::Test <T>::Local&);
Change this to:
friend bool operator == (const Local& a, const Local& b) {
return equals(a, b);
}
};
};
template <class T> bool operator == (const typename Test <T>::Local& a,
const typename Test <T>::Local& b)
{
return a.x == b.x;
}
Replace this by:
template <class T>
bool Test<T>::Local::equals (const Local& a, const Local& b)
{
return a.x == b.x;
}
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]