專業函數模板我基本上是有std::integral_constant
,包括變量的模擬版本,我想專門從Base<T>
派生這些類的函數模板,就像這樣:爲模板派生
template<class T> struct Base{
typedef T type;
T t;
};
template<class T> struct A : Base<T>{
static constexpr T value = 1;
};
template<class T> struct B : Base<T>{
static constexpr T value = 2;
};
struct Unrelated{};
// etc.
template<class T> void foo(T t){
//I would like to specialize foo for A and B and have a version for other types
}
int main(){
foo(A<float>());//do something special based on value fields of A and B
foo(B<float>());
foo(Unrelated()); //do some default behavior
}
這裏有主要問題:
- 我不能包括
value
爲模板,我期待T = double
,float
,或其他一些非整數類型(否則我只是延長std::integral_constant
) - 我不能用乾淨的
std::is_base
我會做std::is_base<Base<T::type>,T>
- 做
foo(Base<T>&)
不會讓我看到value
,我不想不得不求助於虛擬value()
函數(或反射)。 - 很顯然,我想避免每個派生類都專門化foo。
我認爲答案在於使用is_base
,但無論如何嘗試使用它,我都無法使其工作。有沒有更簡單的方法我失蹤?
當然,你有一個或兩個錯字。 'template struct A:Base {'應該是'template struct A:Base {'。這是你的全部問題嗎? –
+1清楚地表達了第一個*你正在嘗試做什麼,然後*如何*你試圖做到這一點,並最終問*你應該怎麼做,你應該做你想做的事情。 –
另外,閱讀[this](http://www.gotw.ca/publications/mill17.htm) –