Re: template friend for operator- How to
On 7/8/2014 5:46 AM, Helmut Jarausch wrote:
I have a problem with a friend declaration of the dyadic operator- IFF there is a monadic operator-, as well.
Note that the friend declaration below is accepted if and only if the definition of the monadic operator
is removed.
Actually, if you move it *after* the friend declaration, it will likely
work as well.
Many thanks for a hint,
Helmut
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:
static const INT p = P;
private:
INT val;
public:
Zp() : val(0) {}
Zp( INT x ) : val(x%p) { if (x < 0 ) x+= p; }
Zp operator-() const { return Zp(p-val); } // if this is deleted, it works <<<<<
friend Zp<INT,P> operator- <>(const Zp<INT,P>& a, const Zp<INT,P>& b);
/* gcc-4.9.0 says
Quest_Templ.C:22:28: error: declaration of 'operator-' as non-function
friend Zp<INT,P> operator- <>(const Zp<INT,P>& a, const Zp<INT,P>& b);
^
Quest_Templ.C:22:28: error: expected ';' at end of member declaration
Quest_Templ.C:22:30: error: expected unqualified-id before '<' token
friend Zp<INT,P> operator- <>(const Zp<INT,P>& a, const Zp<INT,P>& b);
*/
};
I tried your code with VC++ 2013, and found the same behavior. I must
have something to do with name resolution, which doesn't involve
arguments, unfortunately. It is fixed if you move the unary (what you
call "monadic") operator declaration/definition *after* the 'friend'
declaration.
HTH
V
--
I do not respond to top-posted replies, please don't ask