Member functions of class templates (C++ only)

You may define a template member function outside of its class template definition.

When you call a member function of a class template specialization, the compiler will use the template arguments that you used to generate the class template. The following example demonstrates this:
template<class T> class X {
   public:
      T operator+(T);
};

template<class T> T X<T>::operator+(T arg1) {
   return arg1;
};

int main() {
   X<char> a;
   X<int> b;
   a +'z';
   b + 4;
}
The overloaded addition operator has been defined outside of class X. The statement a + 'z' is equivalent to a.operator+('z'). The statement b + 4 is equivalent to b.operator+(4).
C++11
You can use trailing return types for template member functions, including those that have the following kinds of return types:
  • Return types depending on the types of the function arguments
  • Complicated return types
For more information, see Trailing return type (C++11).
C++11


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us