C++ 螺纹本地C++;11,VS2015中的错误C2492
我有这样的课: 在CustomAllocator.h文件中:C++ 螺纹本地C++;11,VS2015中的错误C2492,c++,visual-studio-2015,C++,Visual Studio 2015,我有这样的课: 在CustomAllocator.h文件中: namespace MemoryMgmt { class PoolMemory { .... } } class CustomAllocator { public: void * operator new(size_t i_size); void operator delete(void*, void*); virtual ~CustomAllocator(){}; #prag
namespace MemoryMgmt
{
class PoolMemory
{
....
}
}
class CustomAllocator
{
public:
void * operator new(size_t i_size);
void operator delete(void*, void*);
virtual ~CustomAllocator(){};
#pragma warning(suppress: 4251)
static thread_local MemoryMgmt::PoolMemory memory_manager_current;
};
inline void * CustomAllocator::operator new(size_t i_size)
{
return memory_manager_current.AllocateChars(i_size);
}
inline void CustomAllocator::operator delete(void * , void*)
{
//Should not be used
ASSERT(!"Placement delete should not be used for memory objects");
}
在CustomAllocator.cpp文件中:
#include <CustomAllocator.h>
thread_local MemoryMgmt::PoolMemory CustomAllocator::memory_manager_current;
我使用visual studio 2015,我想这个版本完全支持它 当您同时使用
\u declspec(dllexport)
和\u declspec(thread)
时,会发生此错误。在您的情况下,memory\u manager\u current
是static
类成员,用作线程本地存储。但是,我强烈怀疑该类可能已经声明了属性\uuu declspec(dllexport)
(尽管在您文章的代码中没有看到)
您可以安全地将内存管理器\u current
移出类,并将其作为类CustomAllocator
的实现文件中的全局变量
希望这能有所帮助。在创建DLL时,您似乎无法使用
线程\u local
(MS称之为“线程存储持续时间”)。来自:“线程变量的地址在运行时之前是未知的,因此它无法链接到DLL导入或导出。”@Someprogrammerdude感谢您的评论,但根据VisualStudion 2015完全支持它。我错过了什么吗?仅仅因为thread\u local
关键字是为普通程序实现和工作的,并不意味着您仍然可以在DLL中使用它。正如错误告诉你的那样。好吧,还有什么方法可以在DLL中使用它吗?@Someprogrammerdude我在上面的代码中没有提到DLL。你认为OP遗漏了它吗?
error C2492: 'public: static MemoryMgmt::PoolMemory CustomAllocator::memory_manager_current': data with thread storage duration may not have dll interface (compiling source file D:\*******.cpp)