如何在stl模板中使用导出的类(__declspec(dllexport))? [英] how to use an exported class (__declspec(dllexport))in an stl template?

查看:869
本文介绍了如何在stl模板中使用导出的类(__declspec(dllexport))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用导出的类

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

当我这样做,我得到一个警告C4251说m_map:class'std :: map& ,_Ty>'需要有类myclass的客户端使用的dll接口。

关于我如何解决这个问题的任何想法?

Atul

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

因此,如果设置了特定的其他define,则应创建一个使用dllexport的宏。如果不是,则应创建dllimport。

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

在一个常见的标题中:

#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 >;

注意extern这里。
在一个编译单元中声明相同但没有extern并且没有FOO_API,因此:

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.

如果模板是标准库或boost,那么使用yours的代码必须使用相同的版本

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).

这篇关于如何在stl模板中使用导出的类(__declspec(dllexport))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆