Archived

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

squirrel_of_death

templates : damn them to hell

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
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
I''ve read a lot of C++ books now... schilde, waite group, carrano, schaum to name a few. I''m doing most of it on my own ( ahead of classes ). Man, I love C++. I''m getting all this stuff out the way so I can be good at graphics some day soon... Funny thing is, none of the books mentioned template / compiler issues, so it was pretty frustrating. One actually said I could say #include "templateClass.cpp" at the end of the .h file... I don''t know much about compilers yet, but I am surprised that such a useful feature of C++ has these issues! I''ll get back to you on that one when I''m a compiler guru! I''m gonna try the #include .inl file tip. thanks.

Share this post


Link to post
Share on other sites
You''re reading all the wrong authors.

Check out Strustroup, Meyers, Sutter, Alexandrescu, and friends.

Share this post


Link to post
Share on other sites
With regards to broken Intellisense I''ve been using Visual Assitant ( http://www.gamedev.net/columns/books/productreview.asp?productid=219 ) for a while and its Intellisense is much better than the builtin version is seems (both VS.Net and VS.Net03).
About the only thing that it seems to have trouble with is iterators but as i normaly know whats going on i dont see this as much of a problem.

Share this post


Link to post
Share on other sites
Herbert Schildt has a good quick reference called, "C/C++ Programmer''s Reference". It''s a small book, costs $20, and lists all the standard functions and classes in standard C/C++. It also gives a description of what each function and member function does.

It''s the most used book I have purchased. Sure, alot of his books are not the best, but that one is a timesaver. I already know how to code, I just need to know what I have to work with. Though, I have pretty much gotten to the point where I don''t need any references. I just code like I''m writing sentances.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Templates and multiple-inheritance is just stupid hacks to get around limitations, for example linked-list creation.
The java-solution with an Object-class is much nicer.

/Fredrik

Share this post


Link to post
Share on other sites
quote:
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?


no, although if you have to use MSVC6.0 you''ll have a few headaches. don''t be discouraged - you can still do a lot of useful things.

remember when asking questions, the world is bigger than MS.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Templates and multiple-inheritance is just stupid hacks to get around limitations, for example linked-list creation.
The java-solution with an Object-class is much nicer.

/Fredrik


Yes it's SO nice when you have to cross your fingers and downcast everything you get from a container

Granted, I'm not a big fan of multiple inheritance, tho' it can be useful in somesituations.

And did you know that they are working on getting generics to Java?

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction.

[edited by - rohde on May 28, 2003 6:50:23 AM]

Share this post


Link to post
Share on other sites
The trouble with the Java way of things is that

a) You can''t force every member of a list to be of the same type, since absolutely anything can inherit from Object. This is obviously bad from a type-safety point of view.

b) You necessarily have the overhead of a virtual method table, which C++ templates can avoid if you don''t have any virtual methods.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.