Jump to content

Exporting Templates in DLLs


cppman
 Share

Recommended Posts

Hi, how would I go about exporting a templated class in Visual Studio 2008 Express(C++)? I am trying to make a replica of the List class in .NET, for native C++. (I am fully aware of the list type in the STL)

My header file is setup like the following.

#include "bgedecl.h"
        
        namespace Blox
        {
            namespace System
            {
                template <typename T>
                struct _ListEntryData
                {
                    _ListEntryData *pNext;
                    _ListEntryData *pPrev;
                    unsigned long nLocation;
                    T Value;
        
                    _ListEntryData(_ListEntryData *pCur, unsigned long location = 0)
                    {
                        this->pNext = 0;
                        this->pPrev = pCur;
                        this->nLocation = location;
                    }
                };
        
        
        
                template <typename T>
                class BGE_EXPORT List
                {
                public:
                    List();
                    ~List();
                    
                    void Add(T item);
                private:
                    _ListEntryData<T> *pStart;
                    _ListEntryData<T> *pLast;
                    unsigned long nLastLocation;
                };
            }
        }

I then have my C++ source file setup like the following.

#define BGEBUILD
    #include "List.h"
    
    namespace Blox
    {
        namespace System
        {
            template <typename T>
            List<T>::List()
            {
                this->pStart = NULL;
                this->pLast = NULL;
                this->nLastLocation = 0;
            }
    
            template <typename T>
            List<T>::~List()
            {
            }
    
            template <typename T>
            void List<T>::Add(T item)
            {
            }
        }
    }

It compiles fine, however when I link to it with a test project, and try to call the Add method of the List class, it tells me the Add method is an unresolved identifier. I believe this is because you cannot export templates to a DLL unless you have some sort of default template types?

Anyways, here is the source for the test project. (Ignore the failure to use delete for now - this is just a test)

#include "../../BloxGameEngine/List.h"
    #pragma comment(lib, "../../Debug/BloxGameEngine.lib")
    using namespace Blox;
    using namespace System;
    
    int main()
    {
        List<int> *myList = new List<int>();
        myList->Add(100);
    }

So I guess my main question is how I can export this class to a DLL and keep it fully "templated"? Any help would be appreciated, thanks.

I found this, http://www.codeproject.com/KB/cpp/MemberTemplateDLL.aspx however it seems it is specifying each type that can be used with the class - this kind of takes away the purpose of templates.

Edited by chris95219
Link to comment
Share on other sites

Other than the fact that what you ask for makes absolutely no sense, there are, of course, technical reasons. First, templates are a compile-time feature. You can't have the definition of a template in a DLL to be used later because quite simply the compiler has nothing to generate to put into the DLL. Further, you can't even have a template definition in another source module. Templates must be declared and defined in a header file so the compiler can find the information it needs to generate the code. So your code won't even work if you have all the modules as part of a single unit.

It's ironic that you say the example article "takes away the purpose of templates" when that is precisely what you are trying to do with your attempt to somehow get a template into a DLL.

Link to comment
Share on other sites

Other than the fact that what you ask for makes absolutely no sense, there are, of course, technical reasons. First, templates are a compile-time feature. You can't have the definition of a template in a DLL to be used later because quite simply the compiler has nothing to generate to put into the DLL. Further, you can't even have a template definition in another source module. Templates must be declared and defined in a header file so the compiler can find the information it needs to generate the code. So your code won't even work if you have all the modules as part of a single unit.

It's ironic that you say the example article "takes away the purpose of templates" when that is precisely what you are trying to do with your attempt to somehow get a template into a DLL.

Alright, I see. Thanks.

The reason I stated it "takes away the purpose of templates" was because you have to specify each type you were going to accept - which is what I wanted to avoid. But I see now I can't do it how I originally wanted to.

Link to comment
Share on other sites

When you declare "mytemplateclass<int> myobject;" the compiler will generate a class that looks like mytemplateclass but contains int in all the locations where the typename value was used.

If you want to reuse templates, just include the source files in each project. That's about the best you can do.

Semi-on-topic, I think .Net is the only style of programming that supports exportable templates.

Link to comment
Share on other sites

When you declare "mytemplateclass<int> myobject;" the compiler will generate a class that looks like mytemplateclass but contains int in all the locations where the typename value was used.

If you want to reuse templates, just include the source files in each project. That's about the best you can do.

Semi-on-topic, I think .Net is the only style of programming that supports exportable templates.

Yeah, that makes sense. I had the wrong impression of templates. Well, I guess I'll just have to declare and define the class in the header file, as you can't split up the declaration and definition of the template, like Valik said - Unless of course, I misunderstood him.
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...