Jump to content
  • Advertisement
Sign in to follow this  
Ryan_001

Templates and deferred instantiation

This topic is 2514 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.


#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; }


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
Advertisement
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:


#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();
}


should be compliant?

Share this post


Link to post
Share on other sites

Ok, along the same vein, this:


#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();
}


should be compliant?


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

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.


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:
void foo();

int main() {
foo();
}

void foo() {
// Hello
}


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: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) {}

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 bound at point of use 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

What the standard says is that name look up of non-dependent type names is performed using regular look up rules bound at point of use 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.


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!