• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ryan_001

Templates and deferred instantiation

11 posts in this topic

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?
0

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

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?
0

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

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

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

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]
0

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

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

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

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0