Solved

Template member functions of template classes in DLL's

Posted on 1998-02-13
4
469 Views
Last Modified: 2013-12-03
I've learned how to export specific instances of a template class from a DLL, but I now have a problem involving a template class that has a template member function. Here's a simple example: suppose I have a template class Foo, as follows:


// Foo.h
#ifdef BUILDING_FOO
#define EXPORT_TEMPL
#define TEMPL_DECL __declspec(dllexport)
#else
#define EXPORT_TEMPL extern
#define TEMPL_DECL __declspec(dllimport)
#endif

template <class T>
class Foo {
public:
    Foo() { }
    virtual ~Foo() { }
    template <class A>
    static void DoIt( A* pA, T t ) {
        m_datum = t;
        pA->doit(t);
    }
    static T m_datum;
};

EXPORT_TEMPL template class TEMPL_DECL Foo<int>;

// Foo.cpp
int __declspec(dllexport) Foo<int>::m_datum = 123;


Foo has a static data member and a static (template) member function. I export Foo<int> from the DLL, and I have no problem getting clients of the DLL to resolve Foo<int> during linking. I do have a problem accessing Foo<int>::DoIt() however:


// main.cpp
class Bar {
public:
    void doit( int i ) {
        cout << "Bar::doit: i = " << i << endl;
    }
};
void main()
{
    Bar bar;
    Foo<int> foo;
    cout << "foo.m_datum = " << foo.m_datum << endl; // OK
    foo.DoIt( &bar, 456 ); // link err: can't find in DLL
}


The problem is, the linker can't find Foo<int>::DoIt<Bar>() in the DLL. Obviously, this can't be exported from the DLL since Bar is defined by the client and is unknown to the DLL. Is there any way of exporting Foo<int> without exporting the DoIt() template member function so that client code will just instantiate DoIt() during its compilation?
What are my alternatives? (I don't want to turn DoIt() into a non-template member function).
I'm using Visual C++ 5.0 on NT 4.0.
0
Comment
Question by:kfcox
4 Comments
 
LVL 2

Expert Comment

by:anichini
ID: 1411234
I'm pretty sure that VC++ 5.0 doesn't support member templates anyway, so the question is moot, no?

0
 

Author Comment

by:kfcox
ID: 1411235
I've gotten (simple) member templates (such as the one shown in the example I gave) to work just fine ... when there is no DLL exportation involved. What I really need to do is export the static class member variables without exporting the member functions ... and in a way that allows importing the static member variables into clients. For example, if I define Foo as:

template <class T>
class Foo {
public:
    Foo() { }
    virtual ~Foo() { }

    template <class A>
    static void DoIt( A* pA, T t ) {
        m_datum = t;
        pA->doit(t);
    }
    static TEMPL_DECL T m_datum;  // *** export/import m_datum
};


... I'm able to use Foo in clients fine, but the problem that is still hanging me up is that I'm unable to export some other instance of Foo from a separate DLL. In other words, if Foo is defined as part of Foo.DLL (and Foo<int> exported from it), I'm not able to export Foo<something_else> from a separate DLL, say FooUser.DLL because the TEMPL_DECL macro used in specifying m_datum is defined to be __declspec(dllimport) during compilation of FooUser.DLL. Am I making any sense? What I really need to be able to do is export Foo<something_else> from FooUser.DLL!!

0
 
LVL 2

Accepted Solution

by:
mnguyen021997 earned 300 total points
ID: 1411236
try using a def file.  remove all decl_exports and when you try to link you should get a unresolved error with the mangled name.  use the mangled name in the export section of your def file (this is the way mfc does it).
0
 

Expert Comment

by:bergkamp1
ID: 14577820
You can try putting the export syntax ahead of every non-template member of the template class in the class declaration. Then make the template member inline. Then use the EXPORT_TEMPL for each non-template member
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question