Boost logo

Boost :

From: Schaible, Joerg (Joerg.Schaible_at_[hidden])
Date: 2000-10-20 02:04:43


Hi David,

> Sorry, I don't understand. Since all template member
> functions are inline in
> MSVC, how can they interact with anything at link time?

Just a little example hacked down here into the mail to illustrate the
problem for MSVC.

Code that will be provided in an DLL:

==== ExampleClass.hpp ======

#ifdef BUILD_EXAMPLE_DLL
#define EXAMPLE_EXPORT __declspec(dllexport)
#else
#define EXAMPLE_EXPORT __declspec(dllimport)
#endif

#include <boost/smart_ptr.hpp>

class EXAMPLE_EXPORT InternalHelper
{
};

// Have to do export the instanciated template for MSVC to link the DLL
extern template class EXAMPLE_EXPORT boost::shared_ptr<InternalHelper>;

class EXPORT Example
{
boost::shared_ptr<InternalHelper> sptrHelper;
public:
        Example();
        template <class X> void print( const X& x ) { std::cout << x; }
};

======== EOF ====

I omit the .cpp. Now I show you the usage of this class from an application
using the DLL.

==== Application.cpp ======

#include <ExampleClass.hpp>

int main()
{
        Example e;
        e.print(3);
        return 0;
}

======== EOF ====

This will result in a linker error, if my code in the DLL never use this
instanciation of the member with an int. MSVC expects *every* instanciation
of the member template function coming out of that DLL and that is not
possible!

Greetings,
Jörg


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk