cppman Posted March 5, 2008 Posted March 5, 2008 (edited) 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.expandcollapse popup#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 March 5, 2008 by chris95219 Miva OS Project
Valik Posted March 5, 2008 Posted March 5, 2008 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.
Richard Robertson Posted March 5, 2008 Posted March 5, 2008 Darn, I was too late. I wanted to point out the fact that templates aren't complete code yet.
cppman Posted March 5, 2008 Author Posted March 5, 2008 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. Miva OS Project
Richard Robertson Posted March 5, 2008 Posted March 5, 2008 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.
cppman Posted March 5, 2008 Author Posted March 5, 2008 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. Miva OS Project
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now