3
是否可以編寫一個僅用於類類型的部分模板專業化,例如,從特定類繼承或遵守可以通過類型特徵表達的其他約束? 即是這樣的:部分模板專業化侷限於某些類型
class A{}
class B : public A{}
template<typename T>
class X{
int foo(){ return 4; }
};
//Insert some magic that allows this partial specialization
//only for classes which are a subtype of A
template<typename T>
class X<T>{
int foo(){ return 5; }
};
int main(){
X<int> x;
x.foo(); //Returns 4
X<A> y;
y.foo(); //Returns 5
X<B> z;
z.foo(); //Returns 5
X<A*> x2;
x2.foo(); //Returns 4
}
重複的http:// stackoverflow.com/questions/1032973/how-to-partially-specialize-a-class-template-for-all-derived-types? –