thread_local C++11,VS2015 中的错误 C2492 [英] thread_local C++11, Error C2492 in VS2015
问题描述
我有这样的课程:
在 CustomAllocator.h 文件中:
in CustomAllocator.h file:
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 文件中:
in CustomAllocator.cpp file:
#include <CustomAllocator.h>
thread_local MemoryMgmt::PoolMemory CustomAllocator::memory_manager_current;
我在许多不同的库中使用了 CustomAllocator 类.我会收到以下错误(许多错误与此相同):
I used CustomAllocator class in many different libraries. and I will get the following error (many errors same as):
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)
我使用的是 Visual Studio 2015,我想这个版本完全支持它.
I use visual studio 2015 and I guess it is fully supported in this version.
推荐答案
同时使用 __declspec(dllexport)
和 __declspec(thread)
时会出现此错误.在您的情况下, memory_manager_current
是用作线程本地存储的 static
类成员.但是,我强烈怀疑该类可能已使用属性 __declspec(dllexport)
声明(尽管在您帖子的代码中未看到).
This error occurs when you use __declspec(dllexport)
and __declspec(thread)
together. In your case memory_manager_current
is static
class member used as thread local storage. However, I strongly suspect the class might have declared with attribute __declspec(dllexport)
(Although not seen in the code in your post).
您可以安全地将memory_manager_current
移出类,并将其作为class CustomAllocator
的实现文件中的全局变量.
You can safely move memory_manager_current
out of class and make it as global variable in the implementation file of class CustomAllocator
.
希望这会有所帮助.
这篇关于thread_local C++11,VS2015 中的错误 C2492的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!