模板类有两个重载函数

4 c++ templates c++11

我创建了一个对象的模板类,我希望第二个函数在T == int时运行.但我的问题是在sec func()中我想在Object上运行第一个func.

我怎样才能做到这一点?

 template<typename T> Object<T>::func(){

 } 

 template<> Object<int> Object<int>::func(){
        //somecode
        // I want here run the first func() on the object.
 }
Run Code Online (Sandbox Code Playgroud)

谢谢

Cás*_*nan 5

将接口与实现分离是一件简单的事情:

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
}
Run Code Online (Sandbox Code Playgroud)

澄清一下:你所做的不是超载.它被称为模板专业化.如果您为该类型提供了特殊化,则无法为给定类型实例化通用模板.