Jump to content

  • Log In with Google      Sign In   
  • Create Account

Templates and deferred instantiation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Ryan_001   Prime Members   -  Reputation: 1459

Like
0Likes
Like

Posted 27 January 2012 - 08:18 AM

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?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 27 January 2012 - 08:36 AM

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

#3 Ryan_001   Prime Members   -  Reputation: 1459

Like
0Likes
Like

Posted 27 January 2012 - 09:17 AM

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?

#4 wqking   Members   -  Reputation: 756

Like
-1Likes
Like

Posted 27 January 2012 - 10:11 PM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#5 frob   Moderators   -  Reputation: 22731

Like
1Likes
Like

Posted 28 January 2012 - 12:08 AM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#6 wforl   Members   -  Reputation: 165

Like
0Likes
Like

Posted 28 January 2012 - 05:21 AM

Try disabling language extensions.

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

#7 Ryan_001   Prime Members   -  Reputation: 1459

Like
0Likes
Like

Posted 28 January 2012 - 07:03 AM

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.

#8 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 28 January 2012 - 08:12 AM

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


#9 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 28 January 2012 - 08:30 AM

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.

#10 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 28 January 2012 - 09:43 AM

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.

#11 SiCrane   Moderators   -  Reputation: 9669

Like
1Likes
Like

Posted 28 January 2012 - 10:43 AM

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.

#12 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 28 January 2012 - 11:20 AM

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:

"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".


Obviously, it's the expected error.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS