Difference between member functions for a template class defined inside and outside of the class

后端 未结 3 1284
太阳男子
太阳男子 2021-02-14 11:43

Is there a difference between defining member functions for a template class inside the class declaration versus outside?

Defined inside:

template 

        
3条回答
  •  攒了一身酷
    2021-02-14 12:45

    There's no difference, aside from having to type more. That includes the template bit, the inline and having to use more "elaborate" names when referring to the class. For example

    template  class A { 
      A method(A a) { 
        // whatever
      } 
    }; 
    
    template  inline A A::method(A a) { 
      // whatever
    } 
    

    Note that when the method is defined inside you can always omit the template parameter list when referring to A and just use A. When defining it outside, you have to use the "full" name in the return type and in the name of the method (but not in the parameter list).

提交回复
热议问题