Suppose somewhere in the code you have the following instruction
a.setValue(42);
At the same time, the compiler knows that the expression a is of type Test<int> . For example, earlier a was declared as a reference Test<int> &a or as an object Test<int> a .
Please note that it is quite possible that up to this point the compiler has not yet had to look for the definition of the Test pattern. For example, if Π° declared as a link, then such a link can be simply declared as
template <typename> class Test; void foo(Test<int> &a) { ...
That is, to declare a link (or pointer) to Test<int> compiler does not need a full definition of the Test pattern at all. All that is needed is the βmentionβ (declaration) of the fact that Test is some sort of class template with one type parameter.
But as soon as the compiler came across an operator application . to a in
a.setValue(42);
the compiler already needs to instantiate Test<int> and be sure to find the complete definition of the Test template. From the definition of Test<int> compiler will also figure out what setValue .
So, the compiler will search for the definition of the "main" template of the class Test<ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ ΡΠΈΠΏΠΎΠ²ΠΎΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ> . Having found this definition, it will also search for its explicit or partial specializations (for example, Test<int> ).
In your example, the compiler will only find the definition of the βmainβ template.
template <typename T> class Test { ... T setValue(T valueOfUser); };
That is, the compiler will come to the conclusion that type a is this particular template with T == int .
Either from the main template, or from a more suitable specialization (if one was found), he learns what setValue . In your example, this is a template class method. At this point, the compiler recognizes the types, the number of method parameters, and the type of its return value.
It will then search for a template definition for the Test<ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ ΡΠΈΠΏΠΎΠ²ΠΎΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ>::setValue method Test<ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ ΡΠΈΠΏΠΎΠ²ΠΎΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ>::setValue . Suppose the definition was found and looks like
template <typename VASYA> VASYA Test<VASYA>::setValue(VASYA valueOfUser) { value = valueOfUser; }
Comparing the original Test<int> with Test<VASYA> compiler will come to the conclusion that VASYA == int . The fact that the parameter was called T in the definition of the class template and the corresponding parameter in the definition of the method template is called VASYA doesnβt affect anything at all and doesnβt appear in this process.
In addition, the compiler will search for an explicit specialization Test<int>::setValue . If not found, the compiler will use the "main" method template with VASYA == int to instantiate this method. If there is an explicit specialization, the compiler will use the specialization.
That's all.
What kind of "and already somewhere in .cpp determine this method" you are talking about is not clear. Template definitions should be placed in header files. In the .cpp file, you can place only [already] non-template definitions - definitions of explicit specializations, for example.
TTandVOOBSHE_DRUGOE_NAZVANIEare alternative identifiers for the template parameterT(using different identifiers for the same parameter is a bad idea) and is explicitly specified in the lineTest<int>- VTT at