我在将 C++ 代码从 VS2008 迁移到 VS2010 时遇到了问题。到目前为止,我找不到原因的解释,非常感谢您的帮助。
我们有一个自定义内存分配器,它位于一个 dll 中。在其余代码中,我们使用预处理器将分配重定向到我们的函数。下面的简单代码在 VS2008 中编译正确,但在 VS2010 中编译不正确。
在 stdafh.h 中:
#define free my_free
#include <string>
在 VS2010 中我得到:
1>d:\program files\microsoft visual studio 10.0\vc\include\xdebug(62): error C3861: 'free': identifier not found
来自行:
template<class _Ty>
void __CLRCALL_OR_CDECL _DebugHeapDelete(_Ty *_Ptr)
{ // delete from the debug CRT heap even if operator delete exists
if (_Ptr != 0)
{ // worth deleting
_Ptr->~_Ty();
// delete as _NORMAL_BLOCK, not _CRT_BLOCK, since we might have
// facets allocated by normal new.
free(_Ptr);
任何帮助或想法将不胜感激!
摩西
最佳答案
根据 C++ ISO 标准,第 17.4.3.1.1.2 节:
A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.
这意味着 #define
库函数名称有其他含义是不合法的。我猜这恰好在 VS2008 中有效,但是当迁移到 VS2010 时,编译器作者提出了一个无法正常工作的实现。
如果您想重新定义 free
的功能,我建议您通过一个更传统的 channel 来完成,将代码链接到您自己的 C 库实现中,从而更改默认行为。
关于c++ - VS2008 到 VS 2010 迁移 - 一个重大变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4745583/