我只是意识到提出问题有多难…希望我能举出两个例子,足够精确地展示我的问题,并且足够短,不要搞砸一切……至少有编辑的可能性.
所以这就是我目前的情况.当然,我在逻辑/结构(以及无论如何命名方面)方面改变了一点,试图关注我问题的本质:
// MyClass deals with lists (actually several data structures) of the // type MyType which should support different types and has to be // efficiently dealt with. Templating seems just right here class MyClass { ... void doSomething<class MyType>(vector<MyType> someList); ... // At some point I have to extract elements of the type MyType. // The extractor obvIoUsly depends on MyType but it is not possible to // Create a general version that could use templates itself // (unless I use a specialization for each possible MyType) // I am stuck between these two alternatives: // Possibility1: // Let the client pass the right extractor and template it. template<class Extractor,class MyType> void extract(const Extractor& extractor,const string& source,vector<MyType>* dest) { extractor.extract(source,dest); } // Possibility2: // Use a member _extractor of some base type that has to be set // to a specialization. The ExtractorBase has a virtual method // template<T> void extract(const string& source,vector<T>* myType) = 0 // with no definition that is only defined in subclasses wrt certain // postings. ExtractorBase _extractor; template<class MyType> void extract(const string& source,vector<MyType>* dest) { _extractor.extract(source,dest); } }
目前我更喜欢possible1,因为我不需要在Extractor中使用MyType的所有变体以及我想在将来尝试的相关Extractor中使用继承.
另一方面,提取器可能需要复杂的代码和几个成员(类似于映射特定值的某些输入的巨大地图).因此,使用模板不会带来性能提升.特别是使用头文件只有Extractors,甚至可能是内联的仿函数,都是不可能的.在过去,这一直是我的一个强有力的指针,模板化只会增加代码复杂性(必须处理实例化,使客户端代码的生活变得更加困难等),并且我应该尽量避免使用它.
或者有没有第三种可能性我完全没有想到?