简体   繁体   English

导出/定义静态模板专用成员变量C ++

[英]Exporting/Defining Static Template Specialized Member Variable C++

So I am currently exporting a DLL, and I've run into a problem where I have a template class that contains a static char*[] member variable. 因此,我当前正在导出DLL,并且遇到了一个问题,其中有一个包含静态char * []成员变量的模板类。 I want to define this for specializations of that template. 我想为此定义模板的专业化。

example: 例:

MyTemplate.h MyTemplate.h

#include <dll_defines.h>

template <typename T>
class MY_API MyTemplate
{
    public:
        static char const*  m_array[];
}

MySpecializedTemplate.h MySpecializedTemplate.h

#include <dll_defines.h>
#include <MyTemplate.h>

class MY_API Specialized; //pretend its more than just a decl.

template <> char const* MyTemplate<Specialized>::m_array[] =
{
    "Hello, World", 
    "I need help"
};

The issue I am having is when I try and link the DLL in another project, it gives me an error stating: 我遇到的问题是,当我尝试在另一个项目中链接DLL时,它给了我一个错误:

definition of dllimport static data member not allowed 不允许定义dllimport静态数据成员

Is there no way to define a static data member of a template and export it into a DLL?? 没有办法定义模板的静态数据成员并将其导出到DLL中吗?

Okay, so I've realized that I can in fact initialize static template variables in an implementation file. 好的,因此我意识到我实际上可以在实现文件中初始化静态模板变量。 I also realized I SHOULD NOT attach an export or import macro to a template function defined in a header file. 我还意识到我不应该将导出或导入宏附加到头文件中定义的模板函数中。

DONT DO THIS:
template <typename T>
MY_API void foo()
{
    cout << "This won't link, don't try it << endl
}

Realized that the above has a pretty bad problem once MY_API is defined as 意识到,一旦将MY_API定义为

__declspec(dllimport)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM