Thread: templates and DLLs

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    templates and DLLs

    i am new to creating DLLs and the like, and i have a few questions.

    the code i want to package is built heavily upon templates. i am assuming this means that i can only use explicitly declared template parameters into the DLL? For instance, given:
    Code:
    template<typename T>class foo
    {
        T &t;
        public:
        foo(T&_t):
            t(_t)
        {
        }
    };
    the following can be put into a DLL:
    Code:
    void makeFoo(int i)
    {
            new foo<int>(i);
    }
    but i am guessing the following cannot, since DLLs are compiled structures, the template type parameters must be explicitly declared at the time it is compiled:

    Code:
    template<typename T>void makeFoo(T& t)
    {
            new foo<T>(t);
    }

    am i correct in my reasoning?

    also, can someone please refer me to a good reference on this topic? thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are correct - templates need to have the implementation available to the compiler that compiles the code where the template is used.

    I don't know where there is a document describing this, but there's no count of number of instances where someone has posted "why don't my templates work", when using a .h file and a .cpp file to implement the template.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't use the "new" keyword to instantiate the templates. That would result in memory leaks...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Elysia View Post
    And don't use the "new" keyword to instantiate the templates. That would result in memory leaks...
    You can't instantiate a template dynamically? delete wont free its memory?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by valaris View Post
    You can't instantiate a template dynamically? delete wont free its memory?
    Of course you can - but you must assign the value from new into a variable, and then call delete.

    The posted code in that respect is incorrect. There is no pointer to the foo<int> object we created, and thus no way to delete it at a later stage.
    Code:
    void makeFoo(int i)
    {
            new foo<int>(i);
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is more that you create an instance of the object when you don't need it.
    You can force the compiler to instantiate templates and then export those types without needing to create objects with them.
    http://www.geekinterview.com/question_details/16562
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the idea is that i will use container classes within the DLL, but use access methods with POD arguments for the sake of universality.

    i intend to include a done() method in the DLL to invoke the appropriate destructors.

    memory management wasn't really germane to my question so i didn't want to bother including all the trifling details required to make it correct.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by m37h0d View Post
    the idea is that i will use container classes within the DLL, but use access methods with POD arguments for the sake of universality.
    This is the right method, I think. It's generally considered unsafe to allow non-POD data to cross module boundaries (for multiple reasons).

    (The definition of "module" is, of course, specific to a scenario. A single application which just happens to be composed of multiple DLLs might be safe. A DLL intended for third party use is totally different.)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    it is indeed for 3rd party use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 27
    Last Post: 08-21-2008, 11:38 AM
  2. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM