C ++ question conversion function

I tried to use the wrapper boost::function<x>to store and convert my functional objects, and I want to know if there is a compilation check for "whether conversion from an arbitrary type Tto type is performedboost::function<x> .

Here is a sample code:

struct Functor {
   int operator()(int) {
      return 5;
   }
};

// Should not fire static assertion.
STATIC_ASSERT( (IS_CONVERTIBLE<Functor, 
                               boost::function<int (int)> >::value) );

// Should fire static assertion.
STATIC_ASSERT( (IS_CONVERTIBLE<Functor,
                               boost::function<int (std::string)> >::value) );

Now - is there a way to implement validation IS_CONVERTIBLE?


I tried using type attribute checking boost::is_convertible, but it gives truefor any type Functor:

bool value1 = boost::is_convertible<Functor, 
                                    boost::function<int (int)> >::value;

bool value2 = boost::is_convertible<Functor,
                                    boost::function<int (std::string)> >::value;

// At this point both 'value1' and 'value2' equal TRUE.

I also had the following attempts:

// Here the results are also always TRUE.
bool value = boost::is_convertible<Functor, 
                                   boost::function1<int, int> >::value;

// This doesn't work, because unary functions are constructed via
// inheritance (the result is always FALSE).
bool value = boost::is_convertible<Functor, 
                                   std::unary_function<int, int>::value;

I really want to test the possibility of such a conversion at compile time, so if anyone knows how to implement this, I would appreciate it.

Thank.

+3
2

, @James, , - is_callable. / 5 . :)

+1

boost::is_convertible true, , - boost::function, boost::function , .

, , , std::function.

+1

Source: https://habr.com/ru/post/1774835/


All Articles