When does the pattern end?

When does the pattern end? Let's look at this code:

template <class T>
class thatClass
{
   T a, b;
   thatClass (T x, T y) {a = x; b = y;}
};

template <class T>T aFunc(T one, T two)
{
   return one+two;
}

So when does it end template <class T>? Does it always end at the end of a class or function definition, or what? And why can't you use only one template that you specified for both classes and functions, so in this case I could use the template parameter Tfor the function aFuncand for class definition?

+6
source share
1 answer

The scope of the template parameter ends with the template area:

template <class T>
class thatClass
{
   T a, b;
   thatClass (T x, T y) {a = x; b = y;}
}; // << ends here

template <class T>T aFunc(T one, T two)
{
   return one+two;
}  // << ends here

, , T aFunc ?

, / . .

namespace, , , .


, , , :

template <class T>
class thatClass
{
   T a, b;
   thatClass (T x, T y) {a = x; b = y;}
   // A member funcion that uses the same template parameter and accesses 
   // the class member variables
   T aFunc() { return a+b; }
   // A static member funcion that uses the same template parameter and
   // calculates the result from the parameters
   static T aStaticFunc(T one, T two) { return one+two; }
 };
+6

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


All Articles