In general I recommend against returning any instances of templated objects and this includes the STL. You will run into problems later if you attempt to do this from a DLL and Visual Studio will give you a warning about it. I recommend you return a pointer to the STL container or you wrap the object using the template instance and then include a pointer to it in another class that acts as a pass through to the actual instance using the container - the PIMPL idom. Alternatively you can use an interface to the class using the instance of the template so long as you do not attempt to export any part of the template.
PIMPL
Code:
class TemplateUser
{
public:
SomeObject * AddObject(unsigned int ID);
private:
typedef std::map<unsigned int,SomeObject *> ObjectMap;
typedef ObjectMap::iterator ObjectMapIter;
ObjectMap m_objectMap;
};
class TemplateUserPIMPL
{
public:
SomeObject * AddObject(unsigned int ID)
{
SomeObject *pResult = 0;
if (m_pTemplateUser)
{
pResult = m_pTemplateUser->AddObject(pObject);
}
return pResult;
}
private:
TemplateUser *m_pTemplateUser;
};
Interface
Code:
#ifdef TEMPLATERUSER_EXPORTS
#define TEMPLATEUSER_DECL __declspec(dllexport)
#else
#define TEMPLATEUSER_DECL __declspec(dllimport)
#endif
class TEMPLATEUSER_DECL ITemplateUser
{
public:
virtual ~ITemplateUser() { }
virtual SomeObject * AddObject(unsigned int ID) = 0;
};
Code:
class TemplateUser : public ITemplateUser
{
public:
virtual SomeObject * AddObject(unsigned int ID);
private:
typedef std::map<unsigned int,SomeObject *> ObjectMap;
typedef ObjectMap::iterator ObjectMapIter;
ObjectMap m_objectMap;
};
Alternatively you can also choose to export only functions of a class instead of the entire class but you cannot export or return instances of a templated class across a module boundary.