我试图使用boost::function<x>
包装储存和转换我的功能对象,我想知道是否有一个编译支票“从任意类型T
做了转换输入boost::function<x>
存在”。C++函数转换问题
这里是一个代码示例:
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));
现在 - 没有一种方法来实现IS_CONVERTIBLE
检查是否存在?
我试图利用boost::is_convertible
型性状检查,但它产生true
任何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.
我也有以下尝试:
// 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;
我真的希望能够在编译时检查这种转换的可能性,所以 如果有人知道如何实现这一点,我将不胜感激。
谢谢。
Who-hoo-hoo,谢谢。这是我正在寻找的适应版本。 – 2010-11-15 00:44:00