I've written a fixed-precision class, "numeric". This stores the
number of decimal places as a template parameter. I've overloaded all
of the normal numerical operators (an example):
[Dp is the number of decimal places in the left-hand operand, and Dpr
the number in the right-hand operand.]
template<unsigned short Dp>
class numeric {
public:
// Round mode and precision are not assigned.
template<unsigned short Dpr>
numeric& operator = (const numeric<Dpr>& rhs);
template<unsigned short Dpr>
numeric& operator += (const numeric<Dpr>& rhs);
template<unsigned short Dpr>
friend numeric operator + <>(const numeric& lhs,
const numeric<Dpr>& rhs);
};
The implementation is like this:
template<unsigned short Dp>
template<unsigned short Dpr>
numeric<Dp>&
numeric<Dp>::operator = (const numeric<Dpr>& rhs)
{ ... }
template<unsigned short Dp>
template<unsigned short Dpr>
numeric<Dp>&
numeric<Dp>::operator += (const numeric<Dpr>& rhs)
{ ... }
template<unsigned short Dp>
template<unsigned short Dpr>
numeric<Dp> operator + (const numeric<Dp>& lhs,
const numeric<Dpr>& rhs)
{ ... }
While the unary operators are fine, I can't get the binary operator, a
friend, to work. I'm sure this is an issue with the template syntax,
but I can't work out what's wrong. I can't add the first template
list to the declaration, since it's within the class declaration, and
removing the <> makes no difference.
Thanks,
Roger
--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----