1

Topic: Determination of a template after usage

#include <cstdio> const char* name; template <typename T> void set_name (const char* n, T a); void foo () {set_name ("test", 11);} template <typename T> void set_name (const char* n, T a) {name = n;} const char* get_name () {return name;} int main (int argc, char* argv []) {foo (); printf ("%s\n", get_name ()); return 0;} Why this code ? The compiler "collects" requests on ? And whether should under the standard?

2

Re: Determination of a template after usage

Hello, andrey.desman, you wrote: AD> Why this code ? The compiler "collects" requests on ? Templates and  here are not important. Same normal forward-declaration. It is explicitly visible if hardly to modify the code so: #include <cstdio> const char* name;//template <typename T> void set_name (const char* n, int a); void foo () {set_name ("test", 11);}//template <typename T> void set_name (const char* n, int a) {name = n;} int main (int argc, char* argv []) {foo ();}

3

Re: Determination of a template after usage

Hello, watchmaker, you wrote: AD>> Why this code ? The compiler "collects" requests on ? W> Templates and  here are not important. W> same normal forward-declaration. W> It is explicitly visible if hardly to modify the code so: With the forward all is just simple and clear. The declaration, then determination. It is known that to cause,  finds then. There and then in a point of determination of a template there is no function determination as that. It it is necessary  upon usage, but in usage place still there is no determination. I.e. the compiler upon produces  already in the end (a file, a name space?), instead of at the moment of usage, and it is not clear, it and should be, or a singularity of compilers. At studio it too works like as.

4

Re: Determination of a template after usage

Hello, andrey.desman, you wrote: AD> I.e. the compiler upon produces  already in the end (a file, a name space?), instead of at the moment of usage, and it is not clear, it and should be, or a singularity of compilers. At studio it too works like as. It seems that it and works. I do not know, how there under the standard, but the forward  for templates sometimes used when not to manage was. In MSVC2005/2008 works, as well as in gcc the same times, me sufficed

5

Re: Determination of a template after usage

Hello, andrey.desman, you wrote: AD> Why this code ? The compiler "collects" requests on ? AD> And whether should under the standard? De jure this code was incorrect to a C output ++ 11. See Defect Report 993. At present the place in the standard, a describing event, looks so (see head Point of instantiation [temp.point], paragraphs 1 and 8 in the actual version of the standard; selection fat mine): 1 For a function template specialization, a member function template specialization, or a specialization for a member function or static data member of a class template, if the specialization is implicitly instantiated because it is referenced from within another template specialization and the context from which it is referenced depends on a template parameter, the point of instantiation of the specialization is the point of instantiation of the enclosing specialization. Otherwise, the point of instantiation for such a specialization immediately follows the namespace scope declaration or definition that refers to the specialization.... 8 A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. A specialization for a class template has at most one point of instantiation within a translation unit. A specialization for any template may have points of instantiation in multiple translation units. If two different points of instantiation give a template specialization different meanings according to the one-definition rule (6.2), the program is ill-formed, no diagnostic required. I.e. we have the following pattern: #include <cstdio> const char* name; template <typename T> void set_name (const char* n, T a); void foo () {set_name ("test", 11);}//the first point  specializations set_name <int>//(according to paragraph 1, it "follows the namespace scope declaration or definition that refers to the specialization") template <typename T> void set_name (const char* n, T a) {name = n;} const char* get_name () {return name;} int main (int argc, char* argv []) {foo (); printf ("%s\n", get_name ()); return 0;} //the second point  specializations set_name <int>//(according to paragraph 8, "for any such specialization that has a point of instantiation within the translation unit,//the end of the translation unit is also considered a point of instantiation")

6

Re: Determination of a template after usage

Hello, andrey.desman, you wrote: AD> There and then in a point of determination of a template there is no function determination as that. It it is necessary  upon usage, but in usage place still there is no determination. AD> I.e. the compiler upon produces  already in the end (a file, a name space?), instead of at the moment of usage, and it is not clear, it and should be, or a singularity of compilers. I think, the selected fragment should answer: http://www.open-std.org/jtc1/sc22/wg21/ … s.html#993 - it is authorized to the compiler  function in the end of TU, instead of it is immediate after its usage (when yet well its determination).

7

Re: Determination of a template after usage

Hello, watchmaker, you wrote: W>... It is authorized to the compiler  function in the end of TU, instead of it is immediate after its usage (when yet well its determination). The Microsoft always it could. Moreover, here it too compiles it, without everyones forward declaration (it is natural only within the limits of one file): #include <iostream> template <typename Type> int fun1 (int n) {return fun2 <Type> (n);} template <typename Type> int fun2 (int n) {return 0;} int main () {return fun1 <int> (6);} I long used it, yet did not learn that it under the standard and, it appears, is necessary froward declaration

8

Re: Determination of a template after usage

Constructor: the C> De jure this code was incorrect to a C output ++ 11. Whence it follows? The C> See Defect Report 993. What relation points of instantiation have to this point in question? In the C standard ++ 03 (or any by the friend) somewhere it is told, what any points of instantiation should be allocated after determination of an appropriate template? About necessity of determination of a template for the same unit of translation where it is produced implicit , the following there is told: a C ++ 03 [temp]/8: A non-exported template must be defined in every translation unit in which it is implicitly instantiated (14.7.1), unless the corresponding specialization is explicitly instantiated (14.7.2) in some translation unit; no diagnostic is required. About necessity of following template definition and points of instantiation in any strict order I see nothing.

9

Re: Determination of a template after usage

Hello, N I., you wrote: the C>> See Defect Report 993. NI> What relation points of instantiation have to this point in question? It seems that any. They concern resolution of names in templates (more precisely, to resolution dependent names). NI> In the C standard ++ 03 (or any by the friend) somewhere it is told, what any points of instantiation should be allocated after determination of an appropriate template? Found only communication between determination of a class template and points  (paragraph 1 in the actual version of the standard see head Implicit instantiation [temp.inst],): If a class template has been declared, but not defined, at the point of instantiation (17.7.4.1), the instantiation yields an incomplete class type (6.9). [Example: template <class T> class X; X <char> ch;//error: incomplete type X <char> - end example] NI> About necessity of determination of a template for the same unit of translation where it is produced implicit , the following there is told: NI> a C ++ 03 [temp]/8: NI> A non-exported template must be defined in every translation unit in which it is implicitly instantiated (14.7.1), unless the corresponding specialization is explicitly instantiated (14.7.2) in some translation unit; no diagnostic is required. NI> About necessity of following template definition and points of instantiation in any strict order I see nothing. Yes, I too could not find. Generally, about implicit  function templates it is written very little (paragraph 4 see head Implicit instantiation [temp.inst],): Unless a function template specialization has been explicitly instantiated or explicitly specialized, the function template specialization is implicitly instantiated when the specialization is referenced in a context that requires a function definition to exist. On the other hand, in head about resolution of overloads of function templates there are such words (paragraph 5 see head Overload resolution [temp.over],): Only the signature of a function template specialization is needed to enter the specialization in a set of candidate functions. Therefore only the function template declaration is needed to resolve a call for which a template specialization is a candidate. [Example: template <class T> void f (T);//declaration void g () {f ("Annemarie");//call of f <const char *>} The call of f is well-formed even if the template f is only declared and not defined at the point of the call. The program will be ill-formed unless a specialization for f <const char *>, either implicitly or explicitly generated, is present in some translation unit. - end example]