What does it mean that compiler is using two phase lookup in order to compile template class?
Templates are compiled (at least) twice:
Without Instantiation the template code itself is checked for syntax.
Eg: Any syntax errors such as ; etc.
At the time of instantiation(when the exact type is known), the template code is checked again to ensure all calls are valid for that particular type.
Eg: The template might in turn call to functions which might not be present for that particular type.
This is called as Two Phase Lookup.
Related
Today I got the idea that if I have template class with no attributes connected to template arguments an a method that doesn't touch any template arguments also, it should be possible to compile this method separately.
Unfortunately the ARM compiler doesn't allow me to do this.
Is it legal according to standard? Is there any special grammatical construction for this?
I have doubt about template functions.
If i write a normal function like
int function1(int x);
int function1(int x, int y);
two symbol table entry will be made for function1.
each entry represents each overloaded function.
In case of template function, what exactly happens and how it is handled by the compiler.
template<class X>
int function1(X a);
How many symbol table entries will be present for template functions?
There is no such thing as a "template function" in C++. There are "function templates," that is, templates (or perhaps blueprints) for creating functions. Once you accept this, the answer becomes easier to discover.
A template is purely a compilation construct. Each function instantiated from the template is (naturally) a function and thus it will have its own symbol.
Just as with inline functions, if one and the same function (= using the same set of template arguments) is instantiated from the template in different translation units (= different .cpp files), the compiler & linker must ensure these are merged into one, because their addresses must be the same. How they do it is an implementation detail of theirs; the standard just mandates that they have to do it. And I am afraid I don't know the technical details of how this can be done, so I can't provide an example.
When you create an instance of the function template using specific template parameter, the compiler generates one overloaded method. So when you use the method
temlate<class X>
int function1(X a);
to use like
function1<int>(5);
function1<double>(5.0);
Compiler generates
int function1(int a);
int function1(double a);
These are overloaded and there will be 2 symbols.
In fact templates are much like macro, with strong type checking. At compile type function templates are replaced with actual instance. In object code there is no existence of the template function, only the instantiate overloads (if any).
In the case that nobody is using the template to create a compiled function, in fact no entry is created in the symbol table. Otherwise, for each different use of the template (for each type or type combination—in case there are several type parameters to it) one more compiled function and thus one more entry in the symbol table is created.
Templates are evaluated on compile time only; after compiling they do not exist anymore.
For sure "one for each instantiation".
A function template by itself is not a type, or a function, or any other entity.
No code is generated from a source file that contains only template definitions.
In order for any code to appear, a template must be instantiated: the template
arguments must be determined so that the compiler can generate an actual function
link
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Two phase lookup - explanation needed
When I'm using a template class the compiler doesn't show me errors/warning of missing #includes.
For example, If I have a class called "A" and it looks much or less like this :
template<class T>
class A {
void print() const {cout << "Hey I didn't use include for
iostream and It works just fine!!!";}
};
If I remove the template < class T > I get errors of the absence of <iostream >include.
Why the compiler doesn't show these errors when I'm using template class ?
Just to point out, when I say It works I mean It doesn't show me any compilation errors when I'm writing the class but only when I use it as opposed to a non-template class whereas the error shows immediately.
When you write template code, a huge bulk of the syntax checking happens only if and when you make an instance of this class, if it is never used, it is never checked.
To verify this, add this line at the end, A<int>;
More information at Two phase lookup - explanation needed as jrok points out.
Edit:
The linked posts bring up an interesting point, this does error out on gcc and clang even without an instantiation. I guess like myself, you are on MSVC++
When the compiler first parses your template, it is only required to perform the most basic syntax checks and type checking for non-dependent types (types which are not defined in terms of the template arguments). For each member function of a fully specialized templated type, all type-checking on dependent types (those that depend on template arguments) only needs to be done the first time it comes across an expression using that function (for example by calling it). This also means that any member function you don't use (for a particular specialization) of a templated type might not be fully compiled at all.
This is called two-phase name lookup and (as mentioned in the other answers) you can find more information about it here: Two-phase lookup
When you use a template with numerous methods (like vector) and compile your code, will the compiler discard the code from the unused methods?
A template is not instantiated unless it is used, so there is actually no code to discard.
The standard says (14.7.1/10)
An implementation shall not implicitly instantiate a function template, a member template, a non-virtual member function, a member class, or a static data member of a class template that does not require instantiation. It is unspecified whether or not an implementation implicitly instantiates a virtual member function of a class template if the virtual member function would not otherwise be instantiated. The use of a template specialization in a default argument shall not cause the template to be implicitly instantiated except that a class template may be instantiated where its complete type is needed to determine the correctness of the default argument. The use of a default argument in a function call causes specializations in the default argument to be implicitly instantiated.
So if you can avoid making the template's member functions virtual, the compiler will not generate any code for them (and that might work for virtual functions as well, if the compiler is smart enough).
It depends on your optimization level. At higher optimization settings, yes, dead code elimination will most likely occur.
the compiler, optimizers, and the linker can omit and/or reduce that information. each mature tool likely has options specific to dead code elimination.
with templates, the code may not really be created in the first place (unless instantiated).
certainly not all of it will be removed in every scenario, however (rtti is a silent killer). a bit of caution and testing using your build settings can go a long way to help you reduce the binary sizes and dead code.
Smart compilers will exclude it most likely. Long time ago when I played with Borland C++ Builder, I think, it did not throw out unused template class methods. Can not confirm though
What does it mean that compiler is using two phase lookup in order to compile template class?
Templates are compiled (at least) twice:
Without Instantiation the template code itself is checked for syntax.
Eg: Any syntax errors such as ; etc.
At the time of instantiation(when the exact type is known), the template code is checked again to ensure all calls are valid for that particular type.
Eg: The template might in turn call to functions which might not be present for that particular type.
This is called as Two Phase Lookup.