【发布时间】:2010-11-14 23:05:44
【问题描述】:
我尝试使用boost::function<x> 包装器来存储和转换我的功能对象,我想知道是否存在“从任意类型T 到类型boost::function<x> 的转换是否存在”的编译检查。
这是一个代码示例:
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 类型特征检查,但它会为任何Functor 类型生成true:
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;
我真的希望能够在编译时检查这种转换的可能性,所以 如果有人知道如何实现这一点,我将不胜感激。
谢谢。
【问题讨论】:
标签: c++ templates boost metaprogramming type-conversion