Boost logo

Boost :

Subject: [boost] is_explicitly_convertible
From: Daniel Frey (d.frey_at_[hidden])
Date: 2009-12-29 10:46:57


Just in case someone's interested: I implemented is_explicitly_convertible for GCC 4.4+ (4.3 doesn't work). Since it uses lots of C++0x-ish stuff, it is likely not a good addition to boost::type_traits, but I figured the people here might be interested anyway :)

#include <type_traits>

namespace boost
{
  namespace impl
  {
    template< typename T > T make();

    template< typename From, typename To >
    decltype( To( make< From >() ), make< std::true_type >() ) select( int );

    template< typename, typename >
    std::false_type select( ... );
  }

  template< typename From, typename To >
  struct is_explicitly_convertible
    : std::is_same< decltype( impl::select< From, To >( 0 ) ), std::true_type >
  {
  };
}

struct X
{
  X( double& );
  explicit X( int& );
};

#define STATIC_ASSERT( X ) static_assert( X, #X )

int main()
{
  STATIC_ASSERT(( boost::is_explicitly_convertible< double&, X >::value ));
  STATIC_ASSERT(( boost::is_explicitly_convertible< int&, X >::value ));
  STATIC_ASSERT(( !boost::is_explicitly_convertible< void*, X >::value ));
}

Regards, Daniel


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