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
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.
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
Why does the C++ standard define two phase lookup for templates? Couldn't non dependent declarations and definitions' lookups be deferred to the instantiation stage as well?
They could. This is the way most early implementations of templates
worked, and is still the way the Microsoft compiler worked. It was felt
(in the committee) that this was too error prone; it made it too easy to
accidentally hijack a name, with the instantiation in one translation
unit picking up a local name, rather than the desired global symbol. (A
typical translation unit will consist of a sequence of #includes,
declaring the names that everyone should see, followed by implementation
code. At the point of instantiation, everything preceding the point of
instantation is visible, including implementation code.)
The final decision was to classify the symbols in a template into two
categories: dependent and non-dependent, and to insist that the
non-dependent symbols be resolved at the point of definition of the
template, to reduce the risk of them accidentally being bound to some
local implementation symbols. Coupled with the requirement to specify
typename and template when appropriate for dependent symbols, this
also allows parsing and some error checking at the point of definition
of the template, rather than only when the template is instantiated.
This could be viewed as an application of separation of concerns.
At the first phase it just checks for correct syntax, and resolves non-dependent names, as explained here. At the second phase it does something more template specific, verifying if the calls are valid with the specific types. See this [answer] ( Two phase lookup - explanation needed)
Furthermore, if it would be done in only one phase, then it should be done every instantiation. This way is done only once.
If would be done only on the first instantiation, then it would be the same thing, only less structured.