Jump to content
  • Advertisement
Sign in to follow this  
__Daedalus__

Static Libraries and Templates

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

I was reading in Ron Penton's book "Data Structures For Game Programmers" about how template classes need to be defined entirley in the one file (a .h file) due to the way they are compiled. This is opposed to how I normally write classes by putting the class definition in a .h file and the implementation in a .cpp file. The issue is I want to compile all my template classes in to a static library... I am confused because when I include a static library in my projects I have to include a header file for it too (as is normal). The issue is if the headers for my templated classes (which are in my static library) are included with a project then everyone will see the code for them too. How do you include a templated class in a static library without showing the templated class implementation? I'm using Visual C++ 7.0

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by __Daedalus__
I was reading in Ron Penton's book "Data Structures For Game Programmers" about how template classes need to be defined entirley in the one file (a .h file) due to the way they are compiled. This is opposed to how I normally write classes by putting the class definition in a .h file and the implementation in a .cpp file.

The issue is I want to compile all my template classes in to a static library... I am confused because when I include a static library in my projects I have to include a header file for it too (as is normal).

The issue is if the headers for my templated classes (which are in my static library) are included with a project then everyone will see the code for them too.

How do you include a templated class in a static library without showing the templated class implementation?

I'm using Visual C++ 7.0

In short, you don't. The concept of templating is that the code is compiled separately depending on which types or values you instatiate it with. In order to do that, the code that instantiates a template has to somehow "see" the definition of the template. If the code isn't available, that would be pretty difficult, wouldn't it? So, you either have to use the inclusion model, which you describe, or use the keyword export, which VC++ 7 doesn't support. All current export implementations require the code in its C++ form to be visible as well anyway, if I'm not mistaken, so you're pretty much not able to get away with hiding your code. Get over it :p

So, in general, using templates to full effect requires you to have the code visible. However, often times, you don't need the "full effect."

If you are only going to allow people to use the template for a specific set of types known when you create your static library, you can explicitly instantiate the corresponding versions of the templates inside your library and you will be able to use those versions from any code using it without it needing to see the source code. This keeps your templates in their template form and also lets you conceil your code (with the restriction being that you can only use it with your chosen set of types or values, etc).

If you really want to hide your code and allow it to work with user-defined types not known at the time you create your library, you'd pretty much have to ditch templating and go with polymorphism using virtual functions (or use a mix of the two). Have the code work off of poiters to the data and interface with them through the virtual function.

In the end, if you really want the speed that templating can provide, as well as the ability to use it with any type at all, then you're just going to have to settle with leaving your code visible.

Share this post


Link to post
Share on other sites
Quote:
Original post by __Daedalus__
How do you include a templated class in a static library without showing the templated class implementation?
Short of explicit template instantiation, you don't. Your alternatives are obfuscating the code (not a real security measure, but sufficient to deter some) or looking into Comeau C++, the only compiler that supports the export keyword. Of course, that has its own problems, none of which I can elucidate for you, so hopefully someone else will stop by.

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!