我對以下問題感到困惑。我想寫一些特徵結構來測試某個類是否從另一個類派生。這可以通過boost :: is_base_of <>來解決。但是,我想測試的基類有一個免費的未定義模板參數。boost :: is_same模板化基類
下面是一些代碼示例:
template<typename T> class Base {};
class IntDeriv : Base<int> {};
class Foo {};
template< class TestClass >
struct is_derived_from_Base {
// how to create something that does the following and forces the compiler to deduce T
static const bool value = boost::is_base_of< Base<T> , TestClass >::value;
};
int main() {
cout << is_derived_from_Base<Foo> << endl; // should print 0
cout << is_derived_from_Base<IntDeriv> << endl; // should print 1
}
的問題是如何演繹T
爲Base<T>
內is_base_of
。 這可能嗎?我聞到一些enable_if,但我不知道如何把它放在一起。
是的,那也是我的第一次猜測。但是,有一些類似的問題可以通過一些enable_if/SFINAE魔法來解決。關鍵是編譯器實際上不需要推斷T.我只是想知道某些類是否從某個任意T派生出來的Base。 – 2013-04-04 12:50:02
@jrok這是可能的,請參閱我的答案。請注意,該方法可以擴展以查找基地的類型。 – Synxis 2013-04-04 13:32:03