Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

squirrel_of_death

templates : damn them to hell

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

alrighty, I''ve trawled through months of posts here, and have a couple of unresolved questions. 1. Why can''t most compilers yet allow template .h and .cpp files to be seperate? Can this be done ( really ) with MS VC++ 6.0? What about C++.NET 2003? 2. Using the MS VC++ 6.0, if it''s all in the .h file, then when I am using an object ( created from template ), then I don''t get the ''helper'' function list while using the object ( coding it ). You know, you type myObj. ( then MS gives you the list of functions to choose from ) . If all the code is in the .h file, this is disabled, and instead I get the list of member variables, which I don''t want. How can I get the function list? 3. I thought templates were old! (1995!!) Why can''t MS get it right! Dammit!

Share this post


Link to post
Share on other sites
Advertisement
1: Because (almost) nobody supports export. No. No.

2: Sorry, I don''t use vc6. Try deleting your intellisense file and rebuilding your project.

3: Good question. The ISO standard is from 1998, it''s ridiculous that it took this long to get conformant templates from MS.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
1. Because the information necessary for compiling a template into machine code isn''t available until instantiation. For example, the machine code produced from a + b is very different if a and b are ints versus some type of class that implements a non-trivial operator +.

In the traditional model of C/C++ compilation, a single translation unit (i.e. a given .cpp with all includes expanded) is compiled into machine code or something very close to it. With this model, the full source for a template has to be available to the compiler at the point of instantiation so that the proper machine code can be generated from it.

2. Not sure.

3. I think they''re older than that. However, the standard is relatively new. VC++ 6.0 came out around the same time that the standard was ratified. They''ve obviously had a few years to get templates right in .Net but they decided to spend more time on things that they believe are better for their customers. Believe it or not, templates are far from ubiquitous in production code. Many companies steer clear of them due to the platform differences and the perceived complexity.

To allow for templates to be compiled into object code, the compiler would need to generate some sort of intermediate code that can be used later (either by the compiler when it hits the instantiation or by the linker). Doing this isn''t the easiest thing in the world and goes against the way compilers and linkers have tended to work for many years. It''s probably very difficult to argue in a company that needs to make money selling products that a feature such as template implementations in .cpp files that will be used by .000001% of users is more important than some feature that will be used by a much larger portion (such as managed classes or MFC updates).

A common workaround is to place the code that would normally go in a .cpp in a .inl and #include this file in the header.

Share this post


Link to post
Share on other sites
1. Comeau supports export.

2. MSVC has a broken Intellisense in several ways. .Net seems to be better, but 2003 seems to be worse than 2002 a little.

3. Templates aren''t easy to get right.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Another workaround that can be useful in some situations if you know the types you wish to instantiate ahead of time is to create a dummy function in a .cpp that instantiates a template for the type you''d like and calls the functions you need. This forces the given instantiation to be compiled to machine code and available from that .cpp''s .obj. This is sometimes necessary when exporting an instance of a template from a DLL.

For example:
* mytemplate.h - contains template declaration (don''t include .inl)
* mytemplate.inl - contains template implementation
* main.cpp - uses a instance of mytemplate for int (includes .h but not .inl)
* mytemplate_export.cpp - include the .h and .inl and create a dummy function that instantiates it for int and calls any member functions used by main.cpp.

The instantiation exported by mytemplate_export.cpp will be available to code that links to mytemplate_export.obj (main.obj in this case).

Share this post


Link to post
Share on other sites
To force a specific template instantiation to appear in an object file I use the following syntax:

template class EXPORT Event< ShutdownEvent >;

EXPORT is a macro that is the usual __declspec(dllexport) decorator, Event would be the name of the templated class, and the template args (in this case, the subclass), are provided.

Share this post


Link to post
Share on other sites
VC 6 came out right when the standard did, hence it is not fuly standard compliant. For full compliance, try Visual Studio.NET 2003.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
very informative replies, thanks. So then, a further question : it seems to me that templates are wonderful in concept : however, due to all the weirdness, do programmers tend to avoid them as a rule? Do you use the STL and that''s it?

Share this post


Link to post
Share on other sites
1) Export is impossible to implement ideally. Comeau does not support export in any usable sense. Go read about the nitty-gritty details.

2) Add the header file to your project

3) MSVC7/7.1 is very good (I think it supports everything worth supporting); MSVC6 is fairly old, and most other compilers it''s age do not support the full C++ spec well either. g++ 2.x is the only that''s much better. Sun''s compilers are actually worse, Metroworks generally support more, but fail silently which is worse than a lack of support, and Borland''s compilers are usually quite good.

EDG based compilers have very good C++ support (such as Comeau), but they emit C source code, not native code, so you need another compiler before you can do something with them.


Share this post


Link to post
Share on other sites
quote:
Original post by squirrel_of_death
very informative replies, thanks. So then, a further question : it seems to me that templates are wonderful in concept : however, due to all the weirdness, do programmers tend to avoid them as a rule? Do you use the STL and that''s it?


Having to implement them a certain way is SUCH a small price to pay for all of the benefits of templates. Don''t avoid them, just learn them. Which C++ book are you reading?

Share this post


Link to post
Share on other sites

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

Guest
This topic is now closed to further replies.

  • 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!