Re: friend template overload problem
On 22 Mrz., 04:14, Helmut Jarausch <jarau...@skynet.be> wrote:
could anybody please explain to me what's wrong with
the following code
template <typename INT,INT P> class Zp;
template <typename INT,INT P>
Zp<INT,P> operator-(const Zp<INT,P>& a, const Zp<INT,P>& b); // the
binary op-
template <typename INT,INT P>
class Zp {
public:
Zp operator-() const; // that's the unary operator -
// vvvvvvvvvvvv this is binary operator -
friend Zp<INT,P> operator- <>(const Zp<INT,P>& a, const Zp<INT,P>& b);
// this produces
// error: declaration of 'operator-' as non-function
// error: expected ';' before '<' token
};
Many thanks for your help,
This is a tricky one ;-)
The fix is easy, you simply could
(1) invert the unary operator-declaration with the friend
declaration:
template <typename INT,INT P>
class Zp {
public:
friend Zp<INT,P> operator- <>(const Zp<INT,P>& a, const Zp<INT,P>&
b);
Zp operator-() const;
};
The problem is that the friend declaration attempts
to match with the most inner declaration of operator-,
which is the member operator-. This follows from
unqualified lookup rules. Because the above solution
depends on order of declarations, here are alternatives:
(2) If the binary operator would have been located in
another namespace than global namespace, a qualified
declaration would have worked also:
namespace N {
template <typename INT,INT P> class Zp;
template <typename INT,INT P>
Zp<INT,P> operator-(const Zp<INT,P>& a, const Zp<INT,P>& b);
template <typename INT,INT P>
class Zp {
public:
Zp operator-() const;
friend Zp<INT,P> N::operator- <>(const Zp<INT,P>& a, const
Zp<INT,P>& b);
};
}
(3) The following works even with an absolute namespace
qualification and does not have the disadvantages of the previous
ones (be aware that the parentheses around operator-
are mandatory, otherwise it will be parsed as Zp<INT,P>::operator-,
which would specify a member again;-)):
template <typename INT,INT P> class Zp;
template <typename INT,INT P>
Zp<INT,P> operator-(const Zp<INT,P>& a, const Zp<INT,P>& b);
template <typename INT,INT P>
class Zp {
public:
Zp operator-() const;
friend Zp<INT,P> (::operator- <>)(const Zp<INT,P>& a, const
Zp<INT,P>& b);
};
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! ]