R
Roger Leigh
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>:perator = (const numeric<Dpr>& rhs)
{ ... }
template<unsigned short Dp>
template<unsigned short Dpr>
numeric<Dp>&
numeric<Dp>:perator += (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
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>:perator = (const numeric<Dpr>& rhs)
{ ... }
template<unsigned short Dp>
template<unsigned short Dpr>
numeric<Dp>&
numeric<Dp>:perator += (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