Boost logo

Boost :

Subject: [boost] boost::system::error_code::operator unspecified-bool-type()
From: Gruenke, Matt (mgruenke_at_[hidden])
Date: 2011-03-10 17:30:04


I've found boost.system to be very useful for wrapping return codes from 3rd party libraries (e.g. libcurl). However, one problem I've encountered is that error_code's operator for boolean tests assumes that an error value of 0 == success. I've found it puzzling that this logic doesn't reside in the error_category, where it can be customized as needed. For instance, boolean tests could chain to a is_error() virtual method in the error_category, the default implementation of which preserves the current behavior:

  namespace boost
  {
    namespace system
    {
      class error_category : public noncopyable
      {
      public:

        // ...

        virtual bool is_error( int ev ) const
        {
          return ev != 0;
        }

        // ...

      };

      class error_code
      {
      public:

        // ...

        operator unspecified-bool-type() const
        {
          return m_cat->is_error( m_val ) ? unspecified_bool_true : 0;
        }

        // ...

      };
    }
  }

Is there any reason this was not done? What do people think about adding it in a future release?

Matt


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