Date: 2001-08-24 13:33:56
--- In boost_at_y..., scleary_at_j... wrote:
> Having said that, I must also say that I personally think conversion
> functions are evil; they always allow users to hurt themselves.
> conversion to bool and hiding the other conversions will probably
> such as this ambiguous (it will if your "BigInt" has an implicit
> from int's):
> BigInt a;
> int b;
> a + b; // operator+(a, BigInt(b)) or bool(a) + b ?
Fortunately this is no problem.
Thanks to operator.hpp (again)
I have an explicit overload for operator+(const BigInt&, int)
and all other related operators.
> Another method I've seen around is to just overload operator!, so
> can test 'if (!a)' -- but then they can't do 'if (a)'.
This one I also added on request of that user.
> The solution that I would do myself is to provide a member function:
> 'zero()'/'is_zero()', or (if the construction cost is cheap) just
> user do 'if (a == 0)'. It makes the user-side code more explicit,
> more typing, but also forces more clarity.
I can add an operator==(const BigInt&, int)
that avoids conversion;
general comparision with int, however,
will be more expensive than direct test for zero.
Since this test can occur very often during execution of the loop,
the difference might matter.
So the only alternative is b.is_zero().
Actually I have already such a member function.
The problem, however, is that you cannot write
a generic algorithm using b.is_zero() that also works
for built-in types,
see boost::gcd in rational.hpp for an example.
One solution might be to provide
a template<typename T> boost::is_zero(const T& t)
which uses traits to actually
implement as t==0 or t.is_zero() depending on T.
BTW, for testing my unlimited integer classes
I wrote already a seminumerical.hpp
(contained in the big_int archive)
which contains functions like boost::pow, boost::factorial,
this might be a proper place for such a boost::is_zero.
Would there be some need for seminumerical functions?
Would this solution be better than
an implicit conversion to bool for unlimited integer?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk