【发布时间】:2018-09-09 09:06:46
【问题描述】:
我正在尝试导出定义中包含静态变量的函数模板。
.dll/Foo.h:
#ifdef _DLL
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
class API Foo
{
public:
template<typename T>
static T& Get()
{
static T _instance;
return _instance;
}
static void Set();
}
我希望 .dll 和 .exe 的调用引用同一个“_instance”对象。我知道我可以通过在 .cpp 中定义静态变量来做到这一点。但在这种情况下,我正在处理模板,所以我有点卡住了。
编辑: 正在发生的事情的示例..
.dll/Foo.cpp:
void Foo::Set()
{
Foo::Get<int>() = 10;
}
.exe/main.cpp:
int main()
{
auto & x = Foo::Get<int>();
x = 3;
std::cout << x; // 3
Foo::Set();
std::cout << x; // 3 (I want it to be 10)
}
【问题讨论】:
-
@AlanBirtles 我对导出上述函数没有任何问题。我的问题是我想在我的 .dll 和 .exe 项目之间共享函数中定义的静态变量(在本例中为“静态 T _instance”)。
标签: c++ templates static visual-studio-2017 c++17