Class 2 Class Overload Functions

I created an object template class, and I want the second function to be executed if T == int. but my problem is that in sec func () I want to run the first func in Object.

How can i do this?

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

 } 

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

thanks

+4
source share
1 answer

This is a simple question about separating an interface from an implementation:

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
}

To clarify: what you are doing is not overloading . It was called template specialization . It is not possible to create a template template for this type if you have provided specialization for this type.

+5
source

Source: https://habr.com/ru/post/1606832/


All Articles