Jump to content
  • Advertisement
Sign in to follow this  
CJM

VS7.0 - Templated Code

This topic is 4872 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

Hey, Working on my C++ projects in VS7.0, I've run into a brick wall. I basically want to use template specialisation to run particular code [preferably at runtime], but I'm having some issues. Here is the little test source.
template<int T>
class Test
{
public:
	static void Start();
};

void Test<1>::Start(){}

template <int T>
void Test<T>::Start(){}

int g_i;

int main(int argc, char* argv[])
{
	g_i=0;
	Test<g_i>::Start();
	g_i++;
	Test<g_i>::Start();

	Test<1>::Start();
};

Basically, I don't know at compiletime which templates are defined. There are 2 things that I really wouldn't mind to get around somehow. 1, Test<g_i> when g_i=1 calls Test<int T>::Start(), but Test<1> calls Test<1>::Start(). If anyone has a way around this it'd be great [ie, getting g_i==1 to call Test<1>::Start. I was playing around with inheritence and virtual functions instead of static functions, but the code to set up this would be required for each overridden function. 2, the solvable problem, is that I can't seem to put the class functions in an external file. I don't mind keeping the Test<int T> stuff in this file, but I'd really like to be able to put Test<1> elsewhere [ie, have a Test1.cpp, Test2.cpp, Test13.cpp fileset]. I've tried all sorts of code, but can't see any way to get this working and calling the right functions. If anyone has approached this or a similar problem before, any help would really... help. Thanks, CJM

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by CJM
Hey,

Working on my C++ projects in VS7.0, I've run into a brick wall. I basically want to use template specialisation to run particular code [preferably at runtime], but I'm having some issues.


Hi, unfortunately the problem is the "preferably at runtime" part. Templates just don't work that way; their purpose is to generate code (classes and functions) at compile-time. If you need to select what code to run at run-time, use the language features designed for that purpose: polymorphism, function pointers, switch, etc. (though you can use templates to complement those features, or vice versa, to create some very powerful constructs).

Quote:

g_i=0;
Test<g_i>::Start();
g_i++;
Test<g_i>::Start();


Your code shouldn't even compile; if it does, your compiler is pretty badly broken. Templates are strictly a compile-time feature; you can't use values that aren't known until run-time as template non-type arguments. The compiler must know the values in order to generate the code from templates in the first place.

Quote:

2, the solvable problem, is that I can't seem to put the class functions in an external file. I don't mind keeping the Test<int T> stuff in this file, but I'd really like to be able to put Test<1> elsewhere [ie, have a Test1.cpp, Test2.cpp, Test13.cpp fileset]. I've tried all sorts of code, but can't see any way to get this working and calling the right functions. If anyone has approached this or a similar problem before, any help would really... help.


Unless your compiler implements the export keyword (only Comeau does, afaik), all template definitions (declarations aren't enough!) must be available to the compiler in each translation unit they are used. Practically this means that the definitions must be put in headers along with the declarations, though what you can do is put them in a separate file and then #include that file at the end of the header.

Share this post


Link to post
Share on other sites
Hey,

Yeah, I pretty much expected that there'd be no practical way to get it working. The original intention of the system was to allow whoever's using a library that I'm working on to set up a bunch of functions and call them by number, which I guess that I'll just need to make a pre-build step to generate the definitions manually.

So, turns out that the compiler is accessing an <int> template rather than any specific value template, which is why AFAIK what I'm doing compiles. Basically, there's the [MSVC I'd assume] side effect of being able to access (within the Start function, the value of T).

But yeah, there's some VS support for the extern keyword wrt preventing template instantiation, but now because I understand that it's calling <int> rather than a specific <int value> it isn't so much of an issue because I just need to explicitly instantiate the right classes in their own source file [each code piece will only belong to 1 template specialisation - because I'm effectively only using templates as a means for the user to not need to write hundreds of class definitions].

Thanks,

CJM

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!