我試圖使用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