Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use an exported class (__declspec(dllexport))in an stl template?

I am using an exported class

class __declspec(dllexport) myclass  
{  
private:  
template __declspec(dllexport) class std::map<myclass*,int>;    
std::map<myclass*,int>m_map;  
//something  
};    

When I do this, I get a warning C4251 saying m_map:class 'std::map<_Kty,_Ty>' needs to have dll-interface to be used by clients of class myclass.
Any ideas about how i can resolve this ?
Atul

like image 567
Atul Avatar asked Feb 26 '23 18:02

Atul


1 Answers

You should not be using __declspec(dllexport) into the header files that code using your DLL will be using, as they need to be using __declspec(dllimport).

Therefore you should create a macro that uses dllexport if a particular other define is set, and dllimport if it is not.

In a common header somewhere:

#ifdef EXPORTING_FOO
#define FOO_API __declspec(dllexport)
#else
#define FOO_API __declspec(dllimport)
#endif

You can instantiate a template:

extern template class FOO_API Templ<MyClass, int >;

within an included header. Note the extern here. Within one compilation unit declare the same but without the extern and without the FOO_API, thus:

template class Templ<MyClass, int >;

This will mean that the code using your library will not instantiate the template but will be using the one in your library. This is particularly useful when the template has virtual members.

If the template is one from the standard library or boost, the code using yours will have to be using the same version as yours or there could be a serious problem.

Given that in your own example it appears in the private area suggests you want to refactor it out of the interface for your library. Ideally your library should only expose public methods and members other than forward declarations. Private copy-constructors and assignments in order to make a class non-copyable and non-assignable are fine - they are not really part of the implementation they are part of the interface of your class (you are saying that you cannot copy or assign them).

like image 154
CashCow Avatar answered Feb 28 '23 07:02

CashCow