問題描述
我想知道是否有可能發生類似的事情.基本上,我有一個模板化類,它偶爾會使用模板化類的對象.我想將它(或只是一個成員函數)專門用于特定的模板化類,但是該類的通用"形式.
I wondering if something similar to this is possible. Basically, I have a templated class that occasionally takes objects of templated classes. I would like to specialize it (or just a member function)for a specific templated class, but the 'generic' form of that class.
template<typename T, typename S>
class SomeRandomClass
{
//put something here
};
template<typename T>
class MyTemplateClass
{
void DoSomething(T & t) {
//...something
}
};
template<>
void MyTemplateClass< SomeRandomClass<???> >::DoSomething(SomeRandomClass<???> & t)
{
//something specialized happens here
}
用適當的類型(double 等)替換問號是可行的,但我希望它保持通用.我不知道該放什么,因為沒有定義任何類型.我環顧四周,了解了模板模板參數,并嘗試了各種組合都無濟于事.感謝您的幫助!
Replacing the question marks with appropriate types (double, etc) works, but I would like it to remain generic. I don't know what to put there, as any types wouldn't have been defined. I've looked around, and learned about template template parameters, and tried various combinations to no avail. Thanks for the help!
推薦答案
可以像這樣專門化類
template <>
template <typename T,typename S>
class MyTemplateClass <SomeRandomClass<T,S> >
{
void DoSomething(SomeRandomClass<T,S>& t) { /* something */ }
};
只對成員方法進行特化是不可能的,因為特化是針對整個類的,您必須定義一個新類.但是,您可以這樣做
It's not possible to specialize just the member method, because the specialization is on the class as a whole, and you have to define a new class. You can, however, do
template <>
template <typename T,typename S>
class MyTemplateClass <SomeRandomClass<T,S> >
{
void DoSomething(SomeRandomClass<T,S>& t);
};
template <>
template <typename T,typename S>
void MyTemplateClass<SomeRandomClass<T,S> >::DoSomething(SomeRandomClass<T,S>& t)
{
// something
}
拆分聲明和定義.
這篇關于模板類特化,其中模板參數是模板的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!