Solved

Template member functions of template classes in DLL's

Posted on 1998-02-13
4
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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