Sign in to follow this  

Templates and deferred instantiation

This topic is 2180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

While playing around with template I came across something that I was certain should be an error.

[CODE]
#include <iostream>
using namespace std;
template <typename T> T Func (T a) { return Func2(a); }
void main (void) {
int a = 1;
float b = 1;
cout << Func(a) << endl;
cout << Func(b) << endl;
cout << "done" << endl;
getchar();
}
int Func2 (int a) { return 5; }
float Func2 (float a) { return 6.0f; }
[/CODE]

Clearly I don't understand the rules behind template instantiation. To my understanding Func2() should not be 'visible' at the point of the instantiation of Func(), yet this compiles and runs fine using VC++ 2010. What am I missing?

Share this post


Link to post
Share on other sites
I'd have to double check to be 100% positive, but I'm pretty sure this is MSVC being non-standard compliant.

Share this post


Link to post
Share on other sites
Ok, along the same vein, this:

[CODE]
#include <iostream>
using namespace std;
template <typename T> T Func (T a) { return Func2(a); }
int Func2 (int a) { return 5; }
float Func2 (float a) { return 6.0f; }
void main (void) {
int a = 1;
float b = 1;
cout << Func(a) << endl;
cout << Func(b) << endl;
cout << "done" << endl;
getchar();
}
[/CODE]

should be compliant?

Share this post


Link to post
Share on other sites
[quote name='Ryan_001' timestamp='1327677427' post='4906764']
Ok, along the same vein, this:

[CODE]
#include <iostream>
using namespace std;
template <typename T> T Func (T a) { return Func2(a); }
int Func2 (int a) { return 5; }
float Func2 (float a) { return 6.0f; }
void main (void) {
int a = 1;
float b = 1;
cout << Func(a) << endl;
cout << Func(b) << endl;
cout << "done" << endl;
getchar();
}
[/CODE]

should be compliant?
[/quote]

I'm quite confused now.
A test of your two piece of code in MingW GCC 4.6.1, both can be compiled.
However, as far as I understand, bot code is not standard and may fail in two phase template parse.
Hope someone can give clear explanation.

Share this post


Link to post
Share on other sites
What about it do you feel is wrong? That you don't need a forward declaration for them?


Many template details are resolved late in the process. This must be so, otherwise they would lose much of their basic usefulness.


Imagine even basic cases like this:

#include <vector>
...
class mytype {...};
...
std::vector<mytype> foo;

The template type is fully defined before many pieces are in place.

Further, you can write code that only partially implements the functions called by templates; as long as the ones that are actually used are defined it works; the ones that are not actually used are never bound, and don't need to exist at all in spite of being used by the template definition.

Share this post


Link to post
Share on other sites
Try disabling language extensions.

Project Properties->Config->C/C++->Language

Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1327730933' post='4906938']
What about it do you feel is wrong? That you don't need a forward declaration for them?


Many template details are resolved late in the process. This must be so, otherwise they would lose much of their basic usefulness.


Imagine even basic cases like this:

#include <vector>
...
class mytype {...};
...
std::vector<mytype> foo;

The template type is fully defined before many pieces are in place.

Further, you can write code that only partially implements the functions called by templates; as long as the ones that are actually used are defined it works; the ones that are not actually used are never bound, and don't need to exist at all in spite of being used by the template definition.
[/quote]

I was under the impression that the functions/variables/ect... were required to be defined at the point of instantiation. Putting Func2's definition below that of main would mean that the definition of Func2 is after the point of instantiation.

Turning off language extensions only yielded a warning for main (didn't return an int), but it still compiled and ran fine.

Share this post


Link to post
Share on other sites
I don't know what the standard says, but how about this:[code]
void foo();

int main() {
foo();
}

void foo() {
// Hello
}[/code]

Unlike class templates which are instantiated on first use of its members, these are function templates. Functions are resolved by linker - the example above is sufficient to generate the symbol.

Example above could be viewed as this:[code]template <class T > void foo(T);

int main() {
foo_int(1);
foo_float(1.0);
}

void foo_int(int i) {}
void foo_float(float f) {}[/code]

Share this post


Link to post
Share on other sites
What the standard says is that name look up of non-dependent type names is performed using regular look up rules [i]bound at point of use[/i] in the template definition (Section 14.6.3 in both C++03 and C++11). Calling a function that is defined or declared after the template definition shouldn't work. However, this rule is widely not implemented correctly.

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1327761006' post='4907016']
What the standard says is that name look up of non-dependent type names is performed using regular look up rules [i]bound at point of use[/i] in the template definition (Section 14.6.3 in both C++03 and C++11). Calling a function that is defined or declared after the template definition shouldn't work. However, this rule is widely not implemented correctly.
[/quote]

I don't think that section applies for this case, it's about instantiation of templates and resolution of stuff within, not point of instantiation for template itself.

Though honestly, I have absolutely no clue what 14.6.3 or surrounding sections talk about. I think sections below are more relevant, but that's just a guess.

Share this post


Link to post
Share on other sites
In the OP's post, the template function Func() is defined on line 3. That function definition contains a call to Func2(). Because Func2() does not depend on T, it is a non-dependent name. As a non-dependent name it needs to be looked up at the point of use, which is line 3. However, at line 3 Func2() is not in scope, so should not be bound. The point is that look up should not be dependent on point of instantiation. Whatever statements and definitions appear between point of definition and point of instantiation, should not change the meaning of the non-dependent code of a template - at least according to the standard. In practice, this kind of insulation is hard for compiler vendors to implement correctly.

Share this post


Link to post
Share on other sites
I missed that original template declaration has a body that calls Func2, somehow I just jumped to forward declaration-like assumption.

The comeau compiler has the following to say:[quote][code]
"ComeauTest.c", line 1: error: identifier "Func2" is undefined
template <typename T> T Func (T a) { return Func2(a); }
^
detected during instantiation of "T Func(T) [with T=int]" at line 6

"ComeauTest.c", line 1: error: identifier "Func2" is undefined
template <typename T> T Func (T a) { return Func2(a); }
^
detected during instantiation of "T Func(T) [with T=float]" at line 7

2 errors detected in the compilation of "ComeauTest.c".[/code][/quote]

Obviously, it's the expected error.

Share this post


Link to post
Share on other sites
Sign in to follow this