Index: libs/utility/operators.htm =================================================================== RCS file: /cvsroot/boost/boost/libs/utility/operators.htm,v retrieving revision 1.12 diff -u -r1.12 operators.htm --- libs/utility/operators.htm 4 Nov 2002 01:59:31 -0000 1.12 +++ libs/utility/operators.htm 27 Feb 2003 10:51:17 -0000 @@ -340,6 +340,8 @@
indexable<>
bool_testable<>
bool_testable<T>
operator unspecified-bool-type() const
!t
.bool
.addable<T>
addable1<T>
bool_testable
provides a
+ conversion operator to an unspecified value that, when used in boolean
+ contexts, is equivalent to !(!t)
. This allows
+ expressions such as :
+ if (object) + ... ++ +
bool_testable
differs in its
+ use slightly in that it is necessary to publicly derive from it. This is
+ because conversion operators must be members. e.g:
+class Stream : public boost::bool_testable<Stream> +{ +public: + explicit Stream(const char * source); + bool operator!() const; // (Could also be a non-member function) + // operator unspecified-bool-type() auto-generated + // by bool_testable<> +}; + +void f() +{ + if (Stream stream("source.txt")) + { + // use stream ... + } +} ++ +
Although it would perhaps have made more semantic sense to provide
+ bool operator!()
given operator bool()
, it is
+ difficult to implement a safe boolean conversion operator (that prevents
+ accidental conversion to integer types).