Sign in to follow this  

Visual C++ and templates

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

Hi, I'm currently creating a particle system which uses the Policy parttern. Using this pattern implies that my particle system class is a templated one. And to keep my code clean, I would really like to separate my code in a .h and a .cpp. But the "export" keyword doesn't seem to be supported by VC++ ... Is there any way to avoid having only one big .h file with everything in it, when using template classes ? Thx for any advice.

Share this post


Link to post
Share on other sites
As the export keyword is not supported by VC++ (Comeau is a compiler that does support it and the only one I know of that does) you've got to put all template code in header files and AFAIK there's no way around it.

Share this post


Link to post
Share on other sites
joanusdmentia, would you mind explaining that a little more ? Or point me to some article or anything ?

Thx for the replies anyway ^^

Share this post


Link to post
Share on other sites
Two ways:
  • Write it as two separate files as you would normally but instead of #includeing the header at the start of the implementation, #include the implementation at the end of the header. You may also want to change the extension on the implementation from .cpp (or whatever you're using) to something like .tpp or .tpl to distinguish normal implmentation files, which need to be compiled as separate translation units, from template implementation files, which don't.

  • Explicit instantiation. Separate your files as normal and for each instantiated template type include an explicit template instantiation of the form template class /* class name */< /* template arguments */ >;

Note that the full template implementation is required to be visible at the point of explicit template instantiation, so such instantiations are typically placed at the end of the template implementation file.

Σnigma

Share this post


Link to post
Share on other sites
I think VC++ supports the .inl extension for inlined code (not soley) for the use of templated classes like you desire. Check out David Elberly's math library for Wild Magic, he does something very similar to what you want.

Share this post


Link to post
Share on other sites
You can #include a file with any extension. You can have Visual Studio "interpret" any file extension as C++ code as well, though off the top of my head I can't recall where the setting is.

"export" itself isn't the magic bullet people assume it will be anyway. The templates still need to be available when instantiated; it does little to actually remove dependancies and more to hide them. I've found including .inl files to be the best method for writing templates w/out cluttering up the header.

Share this post


Link to post
Share on other sites

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

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