我创建了一个对象的模板类,我希望运行第二个函数,如果是T==int。但我的问题是,在sec func()中,我想对对象运行第一个func。
我该怎么做?
template<typename T> Object<T>::func(){
}
template<> Object<int> Object<int>::func(){
//somecode
// I want here run the first func() on the object.
}谢谢
发布于 2015-09-10 18:07:17
将接口与实现分离是一个简单的问题:
template<typename T> Object<T>::func_impl(){
// Do stuff here
}
template<typename T> Object<T>::func(){
// all func does is call func_impl
func_impl<T>();
}
template<> Object<int> Object<int>::func(){
// Maybe do some stuff...
func_impl<int>(); // ... then call your implementation...
// .. and maybe do some more stuff
}澄清:您要做的是而不是重载。它被称为模板专门化。如果为给定类型提供了专门化,则无法实例化给定类型的通用模板。
https://stackoverflow.com/questions/32508330
复制相似问题