Boost logo

Boost :

From: Sam Partington (sam.partington_at_[hidden])
Date: 2003-11-28 06:20:54


Daniel Frey <mailto:daniel.frey_at_[hidden]> wrote:

> Now add
>
> operator int() const { return 0; }
>
> and see what happens. It compiles, but is definitely not what
> the user
> intended. "if(x)" calls "operator int()" now! The current
> bool_testable doesn't have this problem. Also, the current
> implementation of bool_testable expectes the user to provide operator
> bool(), it adds safety (and operator!()), which is IMHO the most
> obvious, preferable interface and matches the naming conventions of

But we can check that behaviour at compile time with BOOST_STATIC_ASSERT and
is_convertible in the constructor for bool_testable.

Next to the assert we add a comment explaining the pitfall of this technique
for a class with a user defined conversion to integral type, and direct them
to documentation for the alternative technique.

Yes, the interface is not as nice, providing operator! is less intuitive
than providing operator bool. But it also provides more scope for different
implementations. I still think we could provide both implementations, but
you select the implementation using a policy of some kind.

So

struct A : bool_testable<A>
{
  bool operator!() const;
};

would give you a safe_bool derivative.

add an operator int() and it will fail to compile, requesting the user to
change implementation. Along with a forward to the documentation explaining
why.

So then they have to type :

struct A : bool_testable<A, intergral_conversion_compatible>
{
  bool operator!() const;
};

If they later remove the operator int(), then it can fail to compile again,
requesting the user to remove the integral_conversion_compatible parameter.

Sam

--LongSig
******************
This e-mail has been sent from Imagination Technologies Limited.
PowerVR, Metagence, Ensigma and PURE Digital are divisions
of Imagination Technologies Limited.

The information contained in this e-mail, including any attachment,
is confidential and may be legally privileged. It is intended solely
for the addressee(s) and access to this e-mail by anyone else is
unauthorised. If you are not the intended recipient, any disclosure,
copying or distribution or use of the information contained in this
e-mail, is prohibited and may be unlawful. If you have received this
e-mail in error, please notify the sender by return e-mail and then
delete it from your system.

Internet communications cannot be guaranteed to be secure,
error or virus-free. The sender does not accept liability for any errors
or omissions which arise as a result.

Any views expressed in this message are those of the author, except
where the author specifies and, with authority, states them to be the
views of Imagination Technologies Ltd.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk