Boost logo

Boost :

Subject: [boost] [config] unexpected explicit operators conversion on some compilers.
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-03-31 02:49:51


Hi,

Given the following explicit bool conversion

     template<typename Mutex>
     class unique_lock
     {
     public:
         explicit operator bool() const BOOST_NOEXCEPT

I was expecting the following code to compile fail (note the int cast)

     boost::unique_lock<boost::mutex> lk0;
     int i = int(lk0);

It fails with gcc-4.7.0, but compile with gcc-4.6.2 and clang-3.0 (I
have not tested yet with msvc).

When BOOST_NO_EXPLICIT_CONVERSION_OPERATORS is defined I use the bool
safe idiom to emulate the conversion

#if defined(BOOST_NO_EXPLICIT_CONVERSION_OPERATORS)
         typedef void (unique_lock::*bool_type)();
         operator bool_type() const BOOST_NOEXCEPT;
         bool operator!() const BOOST_NOEXCEPT;
#else
         explicit operator bool() const BOOST_NOEXCEPT;
#endif

and in this case the compile fail as desired.

Do we need a new macro to state that even if explicit operators are
provide, there are some unexpected conversions that could be introduced?

Best,
Vicente


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